Sentinel
Sentinel

Reputation: 461

Basic use of getByXpath in HtmlUnit

This has been my best attempt at it:

HtmlUnorderedList unorderedList = (HtmlUnorderedList) page.getFirstByXPath("//ul[@id='inbox-message-list-messages']");

However, that getFirstByXpath returns null. Just learned about using xpath today so I'm sure I'm missing something basic.

code

Upvotes: 3

Views: 8867

Answers (2)

Ahmed Ashour
Ahmed Ashour

Reputation: 5549

I would add that, you compare real Chrome result with HtmlUnit, which may differ.

First you need to ensure you construct with Chrome simulation:

try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) {
}

Then you should see what HtmlUnit sees, by printing:

System.out.println(page.asXml());

Then see the elements, and use the XPath accordingly, as hinted by akhil.

Upvotes: 0

akhil_mittal
akhil_mittal

Reputation: 24157

Once we have a reference to an HtmlPage we can search for a specific HtmlElement using one of get methods or XPath. Check the following example of finding a div by an ID, and getting an anchor by name:

@Test
public void getElements() throws Exception {
    try (final WebClient webClient = new WebClient()) {
        final HtmlPage page = webClient.getPage("http://some_url");
        final HtmlDivision div = page.getHtmlElementById("some_div_id");
        final HtmlAnchor anchor = page.getAnchorByName("anchor_name");
    }
}

And XPath is the suggested way for more complex searches (tutorial) :

@Test
public void xpath() throws Exception {
    try (final WebClient webClient = new WebClient()) {
        final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");

        //get list of all divs
        final List<?> divs = page.getByXPath("//div");

        //get div which has a 'name' attribute of 'John'
        final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']").get(0);
    }
}

Upvotes: 1

Related Questions