0

I have the following code:

    HtmlPage searchPage = mainWebClient.getPage(searchUrl);
    List<HtmlListItem> lis = searchPage.getByXPath("//div[@id='mainContent']//li[contains(@id, 'results-listing')]");
    for (HtmlListItem li : lis) {
        List<Object> linkXPath = li.getByXPath("//a[contains(@class, 'itemlink')]/@href");
    }

On the fourth line I want to search with XPath only in the li and not in the whole page. If i put there: searchPage.getByXPath("//a[contains(@class, 'itemlink')]/@href"); the result will be the same. How to search not in the whole document but only in the li ?

1
  • Please share you html code. Commented Apr 2, 2019 at 6:35

2 Answers 2

1

Use the index rather for each.

searchPage.getByXPath("(//div[@id='mainContent']//li[contains(@id, 'results-listing')])[" + index + "]//a[contains(@class, 'itemlink')]/@href");
Sign up to request clarification or add additional context in comments.

1 Comment

Updated the answer to use the index directly in the xpath. You have to change the loop.
1

Your problem came because the use of an absolute location path (basicaly, a location path starting with /).

You need a relative location path. Use:

descendant::a[contains(@class, 'itemlink')]/@href

Or with abbreviations

.//a[contains(@class, 'itemlink')]/@href

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.