- 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
Locating child nodes of WebElements in selenium.
We can locate child nodes of web elements 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 children with the findElements(By.xpath()) method.
We can identify the child nodes from the parent, by localizing it with the parent and then passing ( ./child::*) as a parameter to the findElements(By.xpath())
Syntax−
parent.findElements(By.xpath("./child::*"))
Let us identify the text of the child nodes of ul node 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 ChildNodes{ 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 t=driver.findElement(By.xpath("//ul[@class='toc chapters']")); //identify child nodes with ./child::* expression in xpath List<WebElement> c = t.findElements(By.xpath("./child::*")); // iterate child nodes for ( WebElement i : c ) { //getText() to get text for child nodes System.out.println(i.getText());} driver.close(); } }
Output
- Related Articles
- Selenium Webdriver Locating Strategies By Class Name
- Can we prevent the collapse of nodes and child nodes in a JTree with Java?
- How to expand JTree row to display all the nodes and child nodes in Java
- Java Program to get the count of child nodes of any node in JTree
- How to remove all child nodes from a parent in jQuery?
- How to use relative xpath for locating a web-element by particular Attribute in Selenium?
- How to handle child windows in Selenium with python?
- Locating Modules in Python
- How to remove all child nodes from a parent using jQuery?
- Locating File Positions in Python
- How to close child browser window in Selenium WebDriver using Java?
- How to remove all child nodes from a parent node using jQuery?
- Program to remove all nodes with only one child from a binary tree in Python?
- Locating and executing Python modules (runpy)
- How will you travel from child to parent with xpath in Selenium with python?

Advertisements