Niko Lang
Niko Lang

Reputation: 559

Find nested elements in selenium webdriver (Java)

I want to select several elements on a site and iterate through them. Of these elements I want to find nested elements with xpath.

List<WebElement> elements = driver.findElements(By.className("aagrRow"));
for(WebElement we: elements){ 
    System.out.println(we.findElement(By.xpath("(//td[@class=\"spreadsheet\"])[24]")).getText());
}

The problem I have is that xpath doesnt seem to search inside the we-element but from the root of the document. What can I do?

Upvotes: 2

Views: 2478

Answers (1)

Grasshopper
Grasshopper

Reputation: 9058

Try this. You need to start with a period at the beginning.

By.xpath("(.//td[@class=\"spreadsheet\"])[24]")

https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html#findElements-org.openqa.selenium.By-

Upvotes: 6

Related Questions