- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get all descendants of an element using webdriver?
We can get all descendants of an element with Selenium webdriver. First of all we need to identify the parent element with help of any of the locators like id, class, name, xpath or css. Then we have to identify the descendants with the findElements(By.xpath()) method.
We can find the descendants from the parent element, by localizing it with the parent and then passing ( .//*) as a parameter to the findElements(By.xpath())
Syntax
element.findElements(By.xpath(".//*"))
Let us identify the tagname of the descendants of ul element in below html code−
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class DescendantElements{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify element WebElement p=driver.findElement(By.xpath("//ul[@class='toc chapters']")); //identify descendants with .//* expression in xpath List d = p.findElements(By.xpath(".//*")); // iterate descendants for ( WebElement j : d ) { //getTagName() to descendant tags System.out.println(j.getTagName()); } driver.close(); } }
Output
- Related Articles
- How to get an attribute value of an element in Selenium Webdriver?
- How to get all options of a dropdown using Python Selenium webdriver?
- How can I get the current contents of an element in webdriver?
- How to verify an attribute is present in an element using Selenium WebDriver?
- Scrolling to element using Webdriver.
- How to get the width of an element using jQuery?
- How to get the height of an element using jQuery?
- How to get HTML content of an element using jQuery?
- How to get all options in a drop-down list by Selenium WebDriver using C#?
- Clicking an element using javascript vs actions vs webdriver?
- Test if an element is focused using Selenium Webdriver.
- How to scroll to element with Selenium WebDriver using C#?
- How to get HTTP Response Code using Selenium WebDriver?
- How to remove all the attributes of an HTML element using jQuery?
- How to check if an element is visible with WebDriver?

Advertisements