Jsoup is not Selecting Script Tag Jsoup is not Selecting Script Tag selenium selenium

Jsoup is not Selecting Script Tag


The selector :contains(text) looks for an element that has that text value. A script doesn't have text, it has data (otherwise the JS would be visible in the browser). You can use the :containsData(data) selector instead.

E.g.:

Elements els = doc.select("script:containsData(accountIndex)");

Here's an example. The Selector documentation has all the handled query types (which is not just strict CSS).


jsoup only supports CSS selectors, and those only allow you to select based on CSS classes and properties of the DOM elements, not their text contents (CSS selector based on element text?). You could try using another framework for parsing and querying the HTML, for example XOM and TagSoup like described here: https://stackoverflow.com/a/11817487/7433999

Or you could add CSS classes to youc script tags like this:

<script class="class1">// script1</script><script class="class2">// script2</script>

Then you can select the script tags again via CSS using jsoup:

Elements elements = document.select("script.class1");