XPath in Selenium - All You Need to Know

It has always been an essential part of automation script development to locate a web element. It has always been a challenging task to find the correct, useful, and accurate locator in an automation test development process. 

This is when XPath comes into the picture and provides its various functions to help write effective XPaths. In this article on XPath in Selenium, we shall learn all that is necessary to understand XPath and excel in that.

What is XPath?

XPath is a Selenium technique that is used to navigate through the HTML structure of a webpage. It is a syntax or language that makes finding elements on a webpage possible using XML path expression.

In Selenium automation, there may be times when elements cannot be found with general locators like ID, name, class, etc. And this is when XPath is used to locate those elements on the webpage. XPath in Selenium may be used on both XML and HTML documents.

XPath in Selenium has a basic format and that is explained below.

Improve Your Earning Potential Further!

DevOps Engineer Masters ProgramExplore Program
Improve Your Earning Potential Further!

Syntax of XPath

XPath in Selenium provides many essential XPath functions and axes to write effective XPaths of the web elements and define unique locators.

XPath_In_Selenium

  • // : to select the current node.
  • tag name: tag name of a specific node.
  • @: to select the attribute.
  • attribute: it is the attribute name of the node.
  • value: it is the value of the node. 

Types of XPath Locators

There are several types of locators to find the element on web pages accurately.

Locators

Types of XPath

There are two different types of XPath:

  1. Absolute XPath
  2. Relative XPath

Absolute XPath

Absolute XPath refers to the direct way of finding an element. The major drawback of Absolute XPath is that if there are any changes in the element's path, then the XPath will fail. 

The XPath begins with a single forward-slash (/), which states that the element can be selected from the root node. 

The syntax of the Absolute XPath looks like this:

Absolute XPath:/html/body/div[1]/div/div[2]/header/div/div[2]/a/img

Relative XPath

In the case of Relative XPath, the path begins from the middle of the HTML DOM structure. Here, the structure starts with a double forward-slash (//) that states that the element can be searched anywhere on the webpage.

Relative XPath enables you to write from the middle of the HTML DOM structure without any need to write a long XPath.

The syntax of the Relative XPath looks like this:

Relative XPath://*[@id=”block-perfecto-main-menu”]/ul/li[6]/a

XPath Functions in Selenium

There may be times that while locating a particular web element using general attributes, there are elements that have similar properties, like the same name or the same class name. This is when the simple XPath strategies are not efficient and the need for XPath Functions arises.

XPath in Selenium provides XPath functions to write efficient XPaths to discover elements uniquely. Let's have a look at all the different functions in Selenium:

XPath Contains() function

The XPath Contains() is a function used to create an XPath expression. It is used if part of the value of any attribute changes dynamically, like login information, etc. The function can navigate to the web element with the partial text present.

//tag_name[contains(@attribute,’value_of_attribute’)]

The Contains() method accepts two parameters:

  • The attribute of the tag must validate to locate the web element.
  • The value of an attribute is a partial value that the attribute must contain.

XPath Text() function

The XPath Text() is a function used to locate the element on a web page using the web element's text. The function proves its worth if the element contains a text, like a label, etc.

//tag_name[text()= ’Text of the element’]

The text() method here returns the text of the web element when identified by the tag_name, and compared with the value provided on the right side.

XPath Starts-with() function

The XPath Starts-with() function is used to find the element in which the attribute value starts with some specific character or a sequence of characters. The function plays a major role while working with the dynamic web pages. 

/tag_name[starts-with(@attribute,’Part_of_Attribute_value’)]

The Starts-with() accepts two parameters:

  1. The attribute of the tag must validate to locate the web element.
  2. The attribute value is the partial value of the attribute with which the attribute is expected to start.

What Are XPath Axes?

All the XML DOM elements are in a hierarchical structure and can be either located using Absolute paths or Relative paths. For this, XPath provides specific attributes called "XPath Axis."

Here, an axis shows a relationship to the current node and helps locate the relative nodes concerning the tree's current node. So, the XPath Axis uses the relation between several nodes to find those nodes in the DOM structure.

Let's have a look at some Axis that helps in locating the elements on the webpage:

XPath_Axes_In_Selenium

Learn about the scope of automation testing, how to automate web applications, and more with the Selenium Certification Training. Enroll now!

Conclusion

By now, you would know everything that is needed to master XPath in Selenium. We started with learning what XPath is, and in that process, we saw its syntax and the types of XPath locators. Then we discovered the two kinds of XPath along with their respective syntax. Moving forth, we had XPath functions in Selenium, where there were three significant functions along with their relevance. Finally, we came across another concept of XPath axes, where we defined several XPath Axes that will help us locate our elements on the webpage.

If you are planning to learn and excel in DevOps, consider enrolling in Simplilearn’s DevOps certification training course. 

Do you have any questions for us? Please mention it in the comment section of the "XPath in Selenium" article and we'll have our experts answer it for you at the earliest!

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.