如何使用webdriver获取元素的所有后代?

时间:2012-12-27 09:30:35

标签: java collections selenium webdriver

这个元素有子元素,那些子元素又有子元素等等。我想得到作为元素后代的所有元素。感谢。

3 个答案:

答案 0 :(得分:78)

试试这个:

(Java)的

List<WebElement> childs = rootWebElement.findElements(By.xpath(".//*"));

(C#)

IReadOnlyList<IWebElement> childs = rootWebElement.FindElements(By.XPath(".//*"));

答案 1 :(得分:5)

试试这个

List<WebElement> allDescendantsChilds = rootWebElement.findElements(By.xpath("//tr[@class='parent']//*"));

以上内容将为您提供 parent tr 的所有后代子元素(不仅是直接子元素)

答案 2 :(得分:-3)

试试这个:

List<WebElement> childs = rootWebElement.findElements(By.tagName(".//*"));