EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

What is XPath in Selenium?

By Yashi GoyalYashi Goyal

Home » Software Development » Software Development Tutorials » Selenium Tutorial » What is XPath in Selenium?

what is XPath in selenium

What is XPath in Selenium?

Xpath or XML Path is an XML expression tag used for finding the elements in a web page while programming test scripts in Selenium Automation Tool. . It can be used to find the position element’s tags like element’s name, element’s ID, link text, a dynamic element using Xpath, CSS path, element’s class name, etc. On the web page of the application being tested using selenium automation. There are two types of Xpath used, namely Absolute Xpath and Realtive Xpath.

Syntax of XPath in Selenium

Below given is the Syntax of XPath in Selenium:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Xpath = //Tagname[@Atrribute=value]

where,

  • //: Used to select the current node.
  • Tagname: Specifies the tag name of a particular node like img, div, input, etc.
  • @: Selects the attribute.
  • Attribute: Specifies the attribute name of the node.
  • Value: Specifies the values of attributes of the node.

Various Locators are used in Xpath to locate the web elements:

  • Id: Finds the web element by the id of that element. (Id is unique for every web element).
  • Name: Finds the web element by the name of that element.
  • Class name: Finds the web element by the class name of that element.
  • Link Text: Finds the web element by the text written on the link of that element.
  • CSS path: Elements having no name, id, class name are find out through the CSS path created by testers/ developers.

Types of XPath in Selenium

There are basically two types of XPath in Selenium which are described below:

1. Absolute XPath

It is one of the easiest and direct ways to locate a web element in a document. It selects an element path from the root node and takes the path until the element covering all the sections in a document, including div, section, li, etc. It starts with a single forward slash ‘/’ specifying the root level. The only disadvantage of using the Absolute Path in Selenium is that if any changes made in the website or a document, then the whole XPath of that element changes and hence the previous path will not work, and the whole program will get failed.

Example:

html/body/div[1]/section/div/div/div/div[2]/div

If any changes made in the HTML document and div elements’ position have been changed, then the previous XPath will get failed.

2. Relative XPath

One of the disadvantages of Absolute XPath, apart from modification, is too lengthy paths; relative paths are a savior in such situations. Relative Path is used to define an element’s path from the middle of the HTML DOM structure. Unlike Absolute XPaths, it is not necessary to define the path from the root level, which in turn reduces the overall length of XPath. It starts with double forward slash ‘//’ ,i.e. Selecting the web element from anywhere in the document.

Popular Course in this category
Selenium Automation Testing Training (9 Courses, 4+ Projects, 4 Quizzes)9 Online Courses | 4 Hands-on Projects | 38+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (6,051 ratings)
Course Price

View Course

Related Courses
Penetration Testing Training Program (2 Courses)Software Testing Training (9 Courses, 2 Projects)

Example:

//input[@id = ‘email’]/div/li/a

XPath Functions in Selenium

Below given are some of the functions of XPath used in Selenium:

 1. Contains(): It is a method that is used in XPath expression and is used to locate the element with the partial text in the case where the text to be searched has too lengthy, and the value of attribute changes dynamically on reload.

Example:

Xpath = //*[contains(@name=’inputButton’)]

2. Following: This method selects all the current node elements in the HTML DOM structure.

Example:

Xpath=//*[@type='password']//following::input[1]

3. Ancestor: This method is used to select all the ancestor elements from the current node. The ancestor can be grandparents, parents of the current node in the HTML document.

Example:

Xpath=//*[text()='Introduction']//ancestor::div

It will find all the ancestors of the current web element match the text criteria ()= ‘Introduction’ having the div tag.

4. Child: This method is used in the scenarios in which we want to select the current node’s child elements in the HTML document.

Example:

Xpath=//*[@id='email']/child::div

5. Proceeding: This method is used to select all the nodes proceeding to the current node or the ones that come before the current node.

Example:

Xpath=//*[@type=button]//preceding::div

6. Following-sibling: This method is used to locate the siblings who are at the same level as the current node.

Example:

Xpath=//*[@type=button]//following-sibling::div

7. Parent: This method is used to locate the current node’s parent in the HTML document.

Example:

Xpath=//*[@id=’password’]//parent::li

8. Self: This method is used to locate itself in the HTML document. Self signifies the current node only.

Example:

Xpath=//*[@id=’email’]//self::input

9. Descendant: This method is used to find the current node’s descendants in the HTML document.

Example:

Xpath=//*[@id=’email’]//descendant::a

10. Start-with: This method is used in the scenarios when we want to match the starting text of the attributes and when we need to locate the web element when the attribute changes dynamically on refreshing and reload a web page.

Example:

Xpath=//label[starts-with(@name, ‘mess_avg’)]

Conclusion

The above mentioned are some of the XPath types and the various functions used in Selenium for XPath. These functions make it easy to work with XPaths as the foremost step for automating locating the web element through XPath. Various locators such as id, name, CSS path, link text are used in the XPath to locate web elements in the web page. Knowledge and different ways of finding XPath are very important when working with automation testing using either Selenium or any other tool.

Recommended Articles

This is a guide to What is XPath in Selenium?. Here we discuss the basic concept, syntax, types, and Functions of XPath in Selenium with its examples. You may also look at the following article to learn more –

  1. Top 10 Uses of Selenium
  2. Install Selenium
  3. Selenium Alternatives
  4. What is Selenium Web Driver?

Selenium Automation Testing Training (9 Courses, 4+ Projects)

9 Online Courses

4 Hands-on Projects

38+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Selenium Tutorial
  • Basics
    • What is Selenium
    • Uses of Selenium
    • Career in Selenium
    • Selenium Software
    • Selenium Architecture
    • Components of Selenium
    • How to Install Selenium
    • Install Selenium IDE
    • Selenium Framework
    • Selenium Commands
    • How to Use Selenium?
    • Selenium Testing
    • Selenium Automation Testing
    • Selenium Load Testing
    • Selenium Grid
    • What is Selenium Web Driver
    • Selenium Webdriver Commands
    • What is XPath in Selenium
    • Assertions in Selenium
    • Selenium Tools
    • Selenium Interview Questions

Related Courses

Selenium Automation Testing Course

Penetration Training Course

Software Testing Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • 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

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

EDUCBA Login

Forgot Password?

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
Book Your One Instructor : One Learner Free Class

Let’s Get Started

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

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

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

Special Offer - Selenium Automation Testing Training (9 Courses, 4+ Projects) Learn More