Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all immediate children and nothing deeper

WebElement body = browser.findElement(By.xpath("//body"));  body.findElement(By.xpath("")); // I want to get all child elements                                  // inside body, but nothing deeper. 

Example document.

<html>   <body>     <div>     </div>     <span>       <table>       </table>     </span>   </body> </html> 

Expected result is div and span. I have no controll over the documents and they vary greatly.

like image 841
heymona Avatar asked Jan 06 '11 01:01

heymona


People also ask

How can I get immediate child in selenium?

Mastering XPath and CSS Selector for Selenium We can get all element's immediate children with css selectors. We have to use findElements() method to get immediate children. This method will return a list of elements matching css selector. In a css selector if we need to traverse from parent to child we use > symbol.

How do I target immediate child CSS?

The child combinator ( > ) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first. Elements matched by the second selector must be the immediate children of the elements matched by the first selector.


1 Answers

("*") gives all the child elements of the context node. So use:

body.findElement(By.xpath("*")); 
like image 187
peter.murray.rust Avatar answered Sep 22 '22 10:09

peter.murray.rust