Tyler
Tyler

Reputation: 3813

HtmlUnit running getbyxpath inside getbyxpath

I have html like so:

<div id="myId">
    <a href="#">
        <div class="name">Item1</div>
        <span class="location">32143|2323</span>
    </a>
    <a href="#">
        <div class="name">Item1</div>
        <span class="location">32143|2323</span>
    </a>
    <a href="#">
        <div class="name">Item1</div>
        <span class="location">32143|2323</span>
    </a>
</html>

Using HtmlUnit for Java, I need to grab the name and location of each anchor. I tried doing a getByXPath to pickup all the anchors, then looping through them with a for and running another getByXpath to get the name in the div. However the individual items in the list are apparently Objects, so I am unable to run a second getByXPath.

I also tried running the first getByXPath using "/a/div[@class='name']" to which I loop through the results which are Objects and I cannot find the proper method for returning the contents of the divs.

Upvotes: 0

Views: 1837

Answers (1)

Nicolas Labrot
Nicolas Labrot

Reputation: 4106

If your xpath select element, you can cast the list to the desired type:

List<HtmlElement> divs = (List<HtmlElement>)document.getByXPath("//a/div[@class='name']");

Upvotes: 3

Related Questions