Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all descendants of an element using webdriver?

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.

like image 810
Code Enthusiastic Avatar asked Dec 27 '12 09:12

Code Enthusiastic


People also ask

How do you get all elements in a div in Selenium?

selenium code: List<WebElement> obj=driver. findElements(By. xpath("//[@id='Container']//*"));//to get all elements in division System.

How can I get the current contents of an element in Webdriver?

You would need to use element. get_attribute('value') for input elements and element. text to return the text node of an element. You could check the WebElement object with element.

How do you get sibling elements in Selenium?

We can find a next sibling element from the same parent in Selenium webdriver. This is achieved with the help of xpath locator. It is important to note that it is only possible to traverse from current sibling to the next sibling with the help of xpath.

What is the use of getOptions () method in Selenium?

We can extract all the options in a dropdown in Selenium with the help of Select class which has the getOptions() method. This retrieves all the options on a Select tag and returns a list of web elements. This method does not accept any arguments.


2 Answers

Try this one:

(Java)

List<WebElement> childs = rootWebElement.findElements(By.xpath(".//*")); 

(C#)

IReadOnlyList<IWebElement> childs = rootWebElement.FindElements(By.XPath(".//*")); 
like image 198
Igor Khrol Avatar answered Oct 09 '22 04:10

Igor Khrol


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

like image 33
Santoshsarma Avatar answered Oct 09 '22 03:10

Santoshsarma