Skip to content Skip to sidebar Skip to footer

Get Content Of List Of Span Elements With Htmlunit And Xpath

I want to get a list of values from an HTML document. I am using HTMLUnit. There are many span elements with the class topic. I want to extract the content within the span tags: &

Solution 1:

If you know you'll always have an <a> then just add it to the XPath and then get the text() from the a.

If you don't really know if you always will have an a in there then I'd recommend to use the .asText() method that all HtmlElement and their descendants have.

So first get each of the spans:

List<?> topics = (List)page.getByXPath("//span[@class='topic']");

And then, in the loop, get the text inside each of the spans:

topic.asText();

Post a Comment for "Get Content Of List Of Span Elements With Htmlunit And Xpath"