I already tried using the ">" syntax but selenium does not accept it, I know there is a way to get it using Xpath but our entire project is written using CSS selectors.
I am trying to store a list that will contain all immediate children of an element but not their children (descendants), when I use the "*" syntax I get all the element's descendants.
How can I get all element's immediate children with css selectors using 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.
The CSS child combinator ( > ) can be used to select all elements that are the immediate children of a specified element. A combinator combines and explains the relationship between two or more selectors.
popupbutton is the fastest.
You should specify a tag to start from... if you want "all element's immediate children", you would simply get all elements, which isn't really what you want.
To get "all immediate children of an element but not their children" for body
, use body > *
.
Or another example, to get all direct descendants of <div id='question'>
, use div#question > *
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With