EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials XML Tutorial XPath Index
Secondary Sidebar
XML Tutorial
  • Xpath
    • What is XPath?
    • XPath namespace
    • XPath for JSON
    • XPath Last Element
    • Xpath Substring
    • XPath First Element
    • XPath local-name
    • XPath Multiple Attributes
    • XPath Matches
    • XPath Position
    • XPath Index
    • XPath regex
    • XPath id contains
    • XPath innertext
    • XPath Multiple Conditions
    • XPath Helper
    • XPath Generator
    • XPath ID
    • XPath Locator
  • Basic
    • What is XML?
    • XML Tags
    • XML URL
    • XPath Sibling
    • XML root element
    • XML Encryption
    • XML Parsing php
    • xml parsing with java
    • Dataset XML
    • XML Parser in C#
    • XML Tree
    • XML boolean
    • XML sitemap
    • XML Array
    • XML reserved characters
    • XML Viewer
    • XML Uses
    • XML Special Characters
    • XML generator
    • XML file format
    • XML DOM
    • XML ampersand
    • XML Mapping
    • XML File
    • XML Element
    • XML HttpRequest
    • XML XSD
    • XML Schema
    • XML Namespaces
    • XML Comments
    • XML Attributes
    • XML Encoding
    • XML Validation
    • XML CDATA
    • XML Database
    • XML Technologies
    • XML Error
    • XML DTD
    • XML Date
    • XML Parsers
    • String in XML
    • XML with CSS
    • XML Versions
    • XML Features
    • XML Commands
    • Servlet web.xml
    • XPath Injection
    • XPath Functions
    • XPath starts-with
    • XPath Selector
    • XPath Count
    • XPath XML
    • XML Parsing in Oracle
    • XML parsing in python

XPath Index

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.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,883 ratings)

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more