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 Locator
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 Locator

Introduction to XPath Locator

XPath locator is used to find an element if the supposed element is not found using general locators utilizing the name, id, etc. Then XPath finds the element by using a web page. It uses the selenium XPath to find an element. So basically, XPath in selenium is nothing but the xml path, which was used to navigate by using the structure of HTML from the web page of XPath.

XPath Locator

What is the XPath Locator?

XPath locator is used in xml as well as html documents for finding the location of elements from the web page by using the DOM structure of HTML. There are different types of XPath locators available in HTML and xml. We are using an ID locator to find the values. It contains two paths, i.e., relative path and absolute path. It is also known as the xml path locator; it is the most commonly used locator in a web driver of selenium that helps to navigate a web page’s structure.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Use of XPath Locator

It is nothing but the language used to locate a node in the documents of XML. It is also used to substitute when we don’t have a proper name or id attribute for the element we want to locate; into the web driver, the syntax of java for locating the element by using XPath is as follows.

Syntax:

findElement (By.XPath ("XPath"))
  • The above example finds element is nothing but the element we have defined in the syntax of the XPath locator. Find element is a method we use in the XPath locator to locate an element.
  • The XPath path will define the actual XPath we locate from the specified node.
  • There are multiple ways available for writing the XPath locator, as follows.

We write the XPath locator in the following ways:

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)
  • By using a single slash
  • By using double slash
  • By using a single attribute
  • By using the last method
  • By using the text method
  • By using the starts_with method
  • By using contains method
  • By using or condition
  • By using and condition
  • By using multiple attributes
  • By using a single attribute

It used the same strategies which were used by web driver and selenium IDE for specifying the location of a particular web element. It allows us to select the elements which were individual attributes, and it also selects the other part of a document of xml by specifying the particular web element location.

XPath Locator Methods

The XPath locator method is used to find the dynamic and complex elements,

1. Following method is used to select all elements from the document of the current node; below is an example of the following method.

Example:

Code:

XPath = //*[@type = 'text']//following::input[1]

Output:

select all elements from the document of the current node

2. The ancestor method will select all ancestor elements from the current node. The example below shows us the ancestor’s element from the current node.

Example:

Code:

XPath = //*[text() = 'Enterprise Testing'] //ancestor::div

Output:

ancestor's element from the current node

3. The child method selects all the child elements from the current node.

Example:

Code:

XPath = //*[@id = XPath locator']//child::li

Output:

child method is used to select all the child element

4. The preceding method of the XPath locator will select all the nodes coming before the current node.

Example:

Code:

XPath = //*[@type = 'XPath locator']//preceding::input

Output:

preceding method of the XPath locator

5. The following sibling methods are used to select the siblings from the context node. Siblings contain the same level from the current node, shown in the below example, which was used to find the element from the current node.

Example:

Code:

XPath = //*[@type = XPath locator]//following-sibling::input

Output:

siblings methods is used to select the siblings

6, The parent method will select the current nose, shown in the below example.

Example:

Code:

XPath = //* [@id = 'parent_node']//parent::div

Output:

XPath Locator - parent method will select the current nose

7. The self XPath locator method is used to select the current node, which indicates the node itself. The below example shows the XPath self-locator method as follows.

Example:

Code:

XPath = //*[@type = 'XPath']//self::input

Output:

self XPath locator method

XPath Locator Types

It requires an efficient approach to locate an element. So we can say that the locator is very important in XPath. Every selenium command in XPath will need the locator to find the web elements to identify the element accurately.

Below are the XPath locator types as follows:

  • ID – This is a prevalent type of XPath locator. We can align each element ID as unique; therefore, it is essential to understand the element.
  • Class name – Class name is used to find the element by using the class name.
  • Name
  • XPath
  • Link text
  • CSS path
  • tag name
  • Partial link text

We are using a web driver with java for the locating strategy of its types of locators.

Below are the locating strategies used by it as follows:

  • Locating strategies by using ID – We are using a driver.findElement (By.id (element_id)) for locating elements by using ID.
  • Locating strategies by using the name – We are using driver.findElement (By.name (element_name)) for locating elements by using the name.
  • Locating strategies by using the class name – We are using driver.findElement (By.className (element_class)) for locating elements by using the class name.
  • Locating strategies by using tag name – We are using driver.findElement (By.tagName (tag name)) for locating elements by using tag name.
  • Locating strategies by using link text – We are using a driver.findElement (By.linkText (link text)) for locating elements by using link text.
  • Locating strategies by using partial link text – We are using a driver.findElement (By.partialLinkText (link text)) for locating elements by using partial link text.
  • Locating strategies by using CSS – We are using a driver.findElement (By.cssSelector (css selector)) for locating elements by using CSS.
  • Locating strategies by using XPath – We are using a driver.findElement (By.XPath (XPath)) for locating elements using XPath.

Conclusion

It is used in xml as well as html documents for finding the location of elements from the web page by using the DOM structure of HTML. It is used to find an element if a supposed element is not found using general locators utilizing the name, id, etc.

Recommended Articles

This is a guide to XPath Locator. Here we discuss the introduction, methods, types, and use of the XPath locator. You may also have a look at the following articles to learn more –

  1. XPath Sibling
  2. XPath Ancestor
  3. XPath regex
  4. XPath namespace
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