Get all immediate children and nothing deeper
34,489
Solution 1
("*")
gives all the child elements of the context node. So use:
body.findElement(By.xpath("*"));
Solution 2
Here's another way to get the direct children of an element:
element.findElement(By.xpath("./*"));
Solution 3
/html/body/*
Will select only immediate children elements of body
.
Do remember that if you copy all these selected nodes, you also copy their content. So, if you do copy-of
, table
will also be produced to the resulting document.
Also, I would recommend to read at least XPath basics, you ask too many similar questions.
Solution 4
The child
instead of descendant
may helps someone.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on December 27, 2020Comments
-
Admin over 3 years
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
andspan
. I have no controll over the documents and they vary greatly.