- 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 identify the nth sub element using xpath?
We can identify the nth sub element using xpath in the following ways −
By adding square brackets with index.
By using position () method in xpath.
Example
import org.openqa.selenium.By; import org.openqa.selenium.Keys; 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(); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // xpath using position() targeting the first element with type text driver.findElement(By.xpath("//input[@type='text'][position()=1]")) .click(); driver.close(); } }
Example
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class RowCount { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://www.tutorialspoint.com/plsql/plsql_basic_syntax.htm"; driver.get(url); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // xpath with index appended to get the data from the row 2 of table List<WebElement> rows = driver.findElements(By.xpath("//table/tbody/tr[2]/td")); System.out.println(“The number of data in row 2 is “+ rows.size()); driver.close(); } }
- Related Articles
- How to identify nth element using xpath in Selenium with python?
- How to find an element using the “XPath” in Selenium?
- How to select element using XPATH syntax on Selenium for Python?
- The xpath of the element keeps changing, how do I find dynamic xpath for this element in Selenium
- Unable to locate an element using xpath error in selenium-java
- Finding the nth power of array element present at nth index using JavaScript
- Delete nth element from headnode using C#
- Finding nth element of the Padovan sequence using JavaScript
- How to access element with nth index in jQuery?
- Using XPATH to search text containing  
- Finding nth element of an increasing sequence using JavaScript
- Find Element and FindElements by XPath in Selenium
- How to remove every Nth element from an array JavaScript?
- How to use relative xpath for locating a web-element by particular Attribute in Selenium?
- Pull an element in sub of sub-array in MongoDB?

Advertisements