You can use the library jsoup for this. Example: to catch all the spans of google, you could do it this way:
Document doc = Jsoup.connect("https://www.google.com").get();
doc.select("span")
.stream()
.forEach(s -> System.out.println(s.text()));
Edit:
To search for an element that contains a certain text, you can use the Pseudo selectors :containsOwn(textoASerBuscado)
or :matchesOwn(regex)
. Example:
Document doc = Jsoup.connect("https://www.google.com").get();
doc.select("span:containsOwn(google)") //Busca todos os spans que contenham "google"
.stream()
.forEach(s -> System.out.println(s.text()));
To learn about other selectors, you can look at documentation of the Selector class
Ok, how can I check if it contains such span with such text on the site? For example: if(s. contains("Example")
– user92401
@Betadarknight I edited and added an example in response to this case.
– Felipe Marinho