简体   繁体   中英

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?

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-

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM