Locating web elements is an important aspect of web application automation and a core activity in automation script development. In order to proceed with this identification of HTML elements, we might need certain definite attributes like id, class, name and so on. However, in certain cases, it might so happen that there might be no unique attributes associated with web elements. This might be the result of poor development practices or due to the presence of dynamic web elements. In such a case, the mechanism through which location of web elements is facilitated is through XPath in Selenium. It acts as a query language which helps extract entities from the Document Object Model (DOM).
In this blog on XPath in Selenium, we will try to look at some of the crucial aspects of XPath Selenium, in terms of what it is, types of XPath as well as the different XPath expressions which can be used for locating different kinds of elements.
What is XPath?
XML Path or XPath in Selenium is a technique to query XML documents and to navigate the HTML structure of a webpage. The language can be used to write XPath query/script which in turn can help in locating and identifying web elements on a webpage using XML Path Expression. XPath in Selenium can be used for HTML as well as XML documents.

The general locators in Selenium may at times, might not suffice to locate all DOM elements of a HTML document. This is where XPath Selenium steps in. By way of helping in providing for dynamic search of elements, it provides for much needed flexibility to adjust a locator as per one’s needs.
Syntax of XPath
XPath=//tagname[@Attribute=’Value’]
//: To select the current node
Tagname: 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
ID: Helps in locating element with the help of the ID of the element
Name: Helps in locating element with the help of the Name of the element
Classname: Helps in locating element with the help of the Classname of the element
XPath: Helps in locating dynamic elements and traverse between different elements of the webpage
Link text: Helps in locating element with the help of the link of a text
CSS Path: Helps in locating elements which have no class, ID or name
Types of XPath
- Absolute XPath
This type is used for directly locating a particular element on the webpage. It is one of the direct ways of finding an element wherein the expression of the XPath is formed by using the selection from the root node. The XPath in this case begins with a single slash (‘/’) and it passes over from the root to the whole Document Object Model (DOM) in order to locate the specific element.
A major shortcoming of this approach is that any change in the path of the element would result in a failed XPath expression.
For instance: /html/body/form/input [5]
This kind of search would begin with the first form tag in the page body and would then select the fifth input field in the form.
2. Relative XPath
In case of Relative XPath in Selenium, the expression starts from the middle of the HTML DOM structure. The expression is marked by beginning it with a double slash (//) which denotes the current node. The search begins from the mentioned tagname and the string value, and helps in searching elements anywhere on the webpage.
The major advantage of this approach is that it is easy to use, is more compact and is less prone to be broken as compared to Absolute XPath Selenium.
For instance: Relative XPath: //input[@name=’email’]
In the given expression of XPath in Selenium, we shall begin our search from the current node which has the tagname as input, whose attribute is name and value is email.