Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all element's immediate children with css selectors using selenium?

Tags:

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.

like image 342
Shiran Avatar asked Nov 23 '11 12:11

Shiran


People also ask

How do you get immediate child element in Selenium?

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.

How do I select all immediate child in CSS?

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.

What is the fastest way to select elements by using CSS selectors?

popupbutton is the fastest.


1 Answers

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 > *.

like image 103
jro Avatar answered Oct 13 '22 09:10

jro