There is this element which has child elements, those child elements again have child elements and so on. I would like to get all elements that are descendants of the element. Thanks.
Asked
Active
Viewed 8.9k times
3 Answers
87
Try this one:
(Java)
List<WebElement> childs = rootWebElement.findElements(By.xpath(".//*"));
(C#)
IReadOnlyList<IWebElement> childs = rootWebElement.FindElements(By.XPath(".//*"));

Kappacake
- 1,826
- 19
- 38

Igor Khrol
- 1,758
- 13
- 18
-
1Is there a way to achieve the same using CSS? – Code Enthusiastic Dec 27 '12 at 09:59
-
`By.cssSelector("*")` - this should work the same way as `By.xpath(".//*")` but not 100% sure that it will not search across all the document (http://www.w3.org/TR/CSS2/selector.html). Why XPath solution doesn't work for you? – Igor Khrol Dec 27 '12 at 10:04
-
1The application I'm working is compatible only with IE. Using CSS is preferable over XPath as XPath execution is slow compared to CSS in IE. – Code Enthusiastic Dec 27 '12 at 10:09
-
I see... Try XPath - it's much more quicker now in IE with WebDriver comparing to Selenium RC. But still there are some issues with performance on very complex XPath-expressions. – Igor Khrol Dec 27 '12 at 10:14
-
Okay. Thank you for the effort. – Code Enthusiastic Dec 27 '12 at 10:28
-
This will gives you all the available elements in that page. – Santoshsarma Dec 27 '12 at 13:21
-
1`rootWebElement` is an instance of `WebElement` class. Class `WebElement` implements `SearchContext` interface as well as class `WebDriver`. So the search is done within some particular element - not within the whole page. – Igor Khrol Dec 27 '12 at 14:16
-
1And if you want only the top-level children of a particular element, just drop one of the slashes: `List
topLevelChildren = rootWebElement.findElements(By.xpath("./*"));`. – EricRobertBrewer Jul 23 '18 at 16:21
5
Try this one
List<WebElement> allDescendantsChilds = rootWebElement.findElements(By.xpath("//tr[@class='parent']//*"));
The above thing will gives you all descendant child elements (not only immediate child) of parent tr

Santoshsarma
- 5,627
- 1
- 24
- 39
-
1what exactly tr is doing there? The element need not to be a table. – Code Enthusiastic Dec 27 '12 at 13:21
-
1For example I've mentioned like tr. In tr place you replace with with parent tag which has child > child like that (ex : tr > td > div > input ) in this case it will give td,div,input elements – Santoshsarma Dec 27 '12 at 13:26
-
Lets say There is a table which has 10 trs, and each row has 10 tds and each td has 10 spans. If the element is table, what I expect to get is 10trs, 100tds and 1000spans but your code just gives 1tr 10tds 100 spans? – Code Enthusiastic Dec 27 '12 at 16:47
-
1NO ..It will give (It is giving also I've verified) all trs (10), tds (100) , spans (1000) .
...<10Spans> <10Spans>....... in this format it will give..just check it. – Santoshsarma Dec 27 '12 at 17:06<10Spans> <10Spans>....... -
Ok, which elmeent does the rootWebElement holds? it holds parent element? and do I've to replace tr with the first child of the parent element? Sorry I am new to XPath, can you explain your code little more? – Code Enthusiastic Dec 28 '12 at 06:43
-
Yes. It holds the parent element. In case of above example table is the parent so you need to specify like List
ele=driver.findElements(By.xpath("//table[@id='tableID']//*")); – Santoshsarma Dec 28 '12 at 07:28
-3
Try this one:
List<WebElement> childs = rootWebElement.findElements(By.tagName(".//*"));

Noel Widmer
- 4,444
- 9
- 45
- 69

Sumit Shitole
- 110
- 1
- 3
- 20