

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding an element in a sub-element in Selenium Webdriver.
We can find an element in a sub-element with Selenium webdriver. First of all we need to identify the element with help of any of the locators like id, class, name, xpath or css. Then we have to identify the sub-element with the findElements(By.xpath()) method.
We can identify the sub-element from the element, by localizing it with the element and then passing the expression (./child::*) as a parameter to the findElements(By.xpath())
Syntax
element.findElements(By.xpath("./child::*"))
Let us identify the tagname of the sub-elements of element ul 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 SubElement{ 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 sub-elements with ./child::* expression in xpath List c = t.findElements(By.xpath("./child::*")); // iterate sub-elements for ( WebElement i : c ) { //getTagName() to get tag of sub-elements System.out.println(i.getTagName()); } driver.close(); } }
Output
- Related Questions & Answers
- Verifying whether an element present or visible in Selenium Webdriver
- Test if an element is focused using Selenium Webdriver.
- Finding an element by partial id with Selenium in C#.
- How to get an attribute value of an element in Selenium Webdriver?
- How to verify an attribute is present in an element using Selenium WebDriver?
- Pull an element in sub of sub-array in MongoDB?
- How to click on hidden element in Selenium WebDriver?
- Send keys without specifying element in Python Selenium webdriver
- How to create a Javascript executor for making an element visible in Selenium Webdriver?
- Test if element is present using Selenium WebDriver?
- How to verify color of a web element in Selenium Webdriver?
- How do I find an element that contains specific text in Selenium Webdriver?
- How to Resolve Stale Element Reference Exception in Selenium WebDriver?
- How do I find an element that contains specific text in Selenium WebDriver (Python)?
- How do you click on an element which is hidden using Selenium WebDriver?
Advertisements