EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials XML Tutorial XPath Index
 

XPath Index

Updated March 28, 2023

XPath Index

 

 

Definition of XPath Index

XPath index is defined as per specific nodes within the node-set for index by using selenium webdriver. We can also mention the index-specific node with the help of a number of indexes enclosed by []. The tag name element UI contains multiple child elements with tag name as li. If we want to identify the child which was second, the parent contains software quality management.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

What is XPath index?

The index expression is used to identify the second child for the parent element created by using the function name as position. For pinpointing the second element we need to append position 2 for the index. We can use the index approach by specifying the tag name in terms of the value of the index which we want to locate. Retrieving the element from the elements of group matching is called the group index. At the time matching expression by using multiple elements then we can use the group index.

How to use XPath index?

In index, we can write the expression with the braces, and then we can write the index outside into the braces. Internally it will be executing the expression first and storing the result in an array of XPath whose index is starting from 1. Last function is used to retrieve the last element which was present in the array. We can also use the position function for retrieving the element position.

The below steps show how to use the index as follows. We are creating the project name as XPathIndex. In the below step, we are creating the project template of index in spring boot.

  1. At the time of creating the project we are providing project group name as com.example, artifact name as XPathIndex, project name as XPathIndex, and selected java version as 11. We are defining the version of spring boot as 2.6.7.                                  

Group – com.example

Artifact name – XPathIndex

Name – XPathIndex

Spring boot – 2.6.7

Project – Maven

Java – 11

Package name – com.example. XPathIndex

Project Description – Project for XPathIndex

Dependencies – Spring web

Packaging – Jar

sder

2. Below we are extracting the project name as XPathIndex and opening the same by using the java development tool name as spring tool suite.

XPath Index dfr

3. In this step we are checking all the project structures and their files are as follows. Suppose this file is not created then we need to create the same manually. In the below example this file is created, so we have no need to create it manually.

XPath Index lkjhdre

4. In this step we are adding a dependency in the index We are adding dependencies as follows.

Code –

<dependency>
<groupId> net.sf.saxon </groupId>
<artifactId> saxon-XPath </artifactId>
<version> 8.7 </version>
</dependency>

Add dependency

  • In the below example, we are implementing the XPath by using the index. In the below example we are creating a class name as XPath_index as follows. In the below example we can see that we have implemented by using the index. We have used the web driver of chrome. We are also using the timeout method with a driver.manage method. We are using the get method and in that get method, we have defined the URL.

Code –

public class XPath_index {
public static void main(String[] args) {
System.setProperty ("webdriver.chrome.driver", "chromedriver.exe");
WebServer driver = new ChromeDriver ()
driver.manage ().timeouts ().implicitlyWait (5, TimeUnit.SECONDS);
((Object) driver).get ("https://example.com/writing.htm");
WebElement t = driver.findElement (By.XPath("//ul[@class='list']/li[2]"));
System.out.println ("Element is: " + t.getText ());
driver.close ();
}
}

How to find the index XPath element?

As we know that there are two types of paths available, first is the absolute path and the second is the relative path. We can use any one of the paths to find the index element. In the case of an absolute path in selenium, the expression is created by using the selection of the root node. It will be starting with the single slash and traversing from the root from the whole DOM for reaching the element which was desired. The disadvantage of using an absolute path is locating an element at the time of development changes in a path. We can also use the basic XPath to find the index element. The basic XPath is a very syntactical and common approach to writing XPath in selenium which is a combination of the attribute value and the tag name. The below example shows how we can find the index XPath element as follows.

Code –

public class XPath_index {
public static void main(String[] args) {
System.setProperty ("webdriver.chrome.driver", "chromedriver.exe");
WebServer XPath = new ChromeDriver ()
XPath.manage ().timeouts ().implicitlyWait (5, TimeUnit.SECONDS);
((Object) XPath).get ("https://example.com/writing.htm");
WebElement t = XPath.findElement (By.XPath ("//ul [@class='list']/li[2]"));
System.out.println ("Element is: " + t.getText ());
XPath.close ();
}
}

XPath index group

XPath index group is nothing but the retrieve a single element from the multiple matching elements. We are retrieving single from the multiple matching elements in the index group. While matching the expression of XPath then we can use or go for index group. Below is the syntax are as follows.

Syntax –

(expression of XPath) [index]

In the syntax of the index group, we are using the expression and the position of the index. In thisx group, we are writing the expression within the braces. We are using the last and position function with the index group to define the index in XPath. Below is the sample html code of the index group as follows.

Code –

<html>
</head>
<body>
<table>
<tr>
<td> <input type = "text" value = "P"> </td>
<td> <input type = "text" value = "Q"> </td>
</tr>
<tr>
<td> <input type = "text" value = "R"> </td>
<td> <input type = "text" value = "A"> </td>
</tr>
</table>
</body>
</html>

Below table shows group index and matching element are as follows, we using matching element as PQRA.

XPath group index Matching element
//input PQRA
(//input)[1] P
(//input)[2] Q
(//input)[3] R
(//input)[4] A
(//input)[last()] A

Conclusion

In index, we can write the expression of XPath with the braces, and then we can write the index outside into the braces. It is defined as per specific nodes within the node-set for XPath index by using selenium webdriver.

Recommended Articles

This is a guide to XPath Index. Here we discuss the Definition, What is XPath index, How to use XPath index and examples with code implementation respectively. You may also have a look at the following articles to learn more –

  1. XPath Descendant
  2. XPath cheat sheet
  3. XPath attribute
  4. XPath Nodes

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW