XPath in Selenium – Syntax, Types, & More
Did you know that the journey of Selenium began at Thoughtworks, a global technology consultancy in Chicago? From 2004 until now, Selenium has aided developers with its wide-ranging functions for web application testing. This blog will discuss one such useful function, XPath, in detail, along with its types and various methods to write it.
What is XPath in Selenium?
XPath is an XML path that is used for navigating a web page’s HTML structure. It contains the path of the element on the web page. XPath in Selenium is a type of locator used to search for elements on a web page. Though it is an XML path, it can be used for both XML and HTML documents. It is a preferred option over the other locators such as class, name, and ID because of its ability to interact with the HTML DOM structure better.
Syntax of XPath
The standard XPath Syntax is as follows:
Xpath=//tagname[@attribute='Value']
Let’s understand the syntax:
- //: It is to select the current node.
- Tagname: It denotes the tag name of the particular node.
- @: It is to select Attribute.
- Attribute: It denotes the attribute name of the node.
- Value: It denotes the value of the attribute.
Types of XPath in Selenium
There are two types of XPath in Selenium. They are as follows:
1. Absolute XPath
A simple form of XPath, it is a direct way to find the elements in the HTML DOM. It starts with a single forward “/”. It denotes that we are selecting the element from the root node. The only disadvantage of the absolute XPath is that it is vulnerable to any changes in the DOM structure. Any change made in the path of the element will lead to the failure of the XPath.
2. Relative XPath
It starts from the middle of the HTML DOM structure; hence, there is no need to write a long XPath. It begins with a double forward slash “//” that denotes the current node. It can search for elements anywhere on the web page. Relative XPath in Selenium is preferred over absolute XPath as it is not a complete path from the root elements and makes it easier to find elements.
Learn software testing to get a better idea about Selenium and its features.
How to Write Dynamic XPath in Selenium?
Dynamic XPath is used to locate elements in XPath whose values change dynamically due to changes in the HTML DOM structure or certain operations being performed on the web page. There are several ways to write dynamic XPath, and we will discuss some of them here.
1. Basic XPath
It is a common approach to XPath in Selenium. It selects nodes from a list of nodes based on attributes like ID, Name, Classname, etc.
Syntax:
Xpath = //tagname[@attribute='Value']
For example, we want to locate the email element on a web page. We will use the following code.
Xpath = //input[@name='email']
2. Contains()
It is used for web elements whose values can change dynamically. It can find the element with partial text.
Syntax:
Xpath = //tagname[contains(@attribute, ‘constantvalue’)]
Take, for example, the following HTML code:
<td>
<input type= “submit” value= “SignUp” name= “btnSignUp”/>
</td>
To find the SignUp button on the web page, we will use the following XPath method in Selenium:
Xpath = input[contains(@type, ‘sub’)]
Here we have used the partial text “sub” for the type “submit”.
3. OR and AND
In OR, two conditions are used to locate an element. One or both of these conditions can be true. But at least one has to be true to find the element.
Syntax:
Xpath = //tagname[@attribute1='value1' or @attribute2='value2']
For example, the following code will select both submit and reset buttons on the web page since the login element has the attribute ‘type’ as submit and the reset element has the attribute ‘name’ as btnReset.
Xpath= //input[@type='submit' or @name='btnReset']
The AND expression also uses two conditions, but here both of them should be true to find the element. If any one of the conditions is false, the expression would fail to find the element.
Syntax:
Xpath = //tagname[@attribute1='value1' and @attribute2='value2']
For example, the following code will locate the login button since it matches both the ‘type’ and ‘name’ attributes.
Xpath = //input[@type='submit' and @name='btnLogin']
4. Starts-with()
It is useful to find elements whose attribute value gets changed by dynamic operations on the web page, such as refreshing the page. Here, the starting text of the attribute is matched to find the element. But it can also find elements with static attribute values.
Syntax:
Xpath = //tagname[starts-with(@attribute,'value')]
For example, the ID for UserID keeps changing, but it starts with a “message”, that is, “User-ID should not be blank.”. The following code will find the element whose ‘id’ starts with ‘message’.
Xpath = //label[starts-with(@id, 'message')]
5. Text()
It is used when there is text defined in an HTML tag and we want to identify the element using that text. Make sure the elements to be located are in string form.
Syntax:
XPath = "//tagname[text()='Text of the web element']"
For example, to find the password element in the blog, we will write the following code:
Xpath = //td[text()='Password']
6. Index
It locates the element using the index value. It is useful in cases where a DOM has multiple input tags for each field value and we want to select only one.
Syntax:
Xpath = //tagname[@attribute='value'] [Index Number]
For example, there is a drop-down menu for selecting age, and we have to select the third option from it. We will use the following code:
Xpath = //div[@class='age group']//select[3]
7. Chained XPath
With this method, we can chain together multiple XPath expressions and use them.
Syntax:
Xpath= //tagename[@attribute1=value1]//tagname2[@attribute2=value2]
For example, we have to locate the checkbox for ‘I agree to the following terms and conditions.’ and then read the terms and conditions by clicking on the text of the hyperlink. We will write the following code:
Xpath = //label[@class='i_agree']//span[@data-amplified='updates']
Writing Dynamic XPath Using the Axes Method
XPath axes are used to search different nodes in the document from the current nodes. They can search the dynamic elements that can not be located using normal XPath in Selenium as they do not have ID, ClassName, Name, etc. Some of the XPath Axes are:
1. Following
It is used when there is a unique attribute tag before the actual web element. “Following” selects all the elements in the document that follow the current node.
Syntax:
Xpath = //tagname[@attribute='value']//following::tagname
2. Ancestor
It locates all ancestor elements, such as a grandparent, parent, etc., of the current node.
Syntax:
Xpath = //tagname[@attribute='value']//ancestor::tagname
3. Child
It selects all child elements of the current node.
Syntax:
Xpath = //tagname[@attribute=’value’]//child::tagname
4. Preceding
It selects all the nodes that come before the current node.
Syntax:
Xpath= //tagname[@attribute='value']//preceding-sibling::tagname
5. Following-Sibling
It selects the following siblings of the specified nodes.
Syntax:
Xpath = //tagname[@attribute='value']//following-sibling::tagname
6. Parent
It selects the parent of the current node.
Syntax:
Xpath = //tagname[@attribute=’value’]//parent::tagname
7. Self
It selects the current node, or ‘self’.
Syntax:
Xpath = //tagname[@attribute=’value’]//self::tagname
8. Descendant
It selects the descendant of the current node.
Syntax:
Xpath = //tagname[@attribute=’value’]//descendant::tagname
Conclusion
XPath in Selenium is surely one of the most advantageous locators used for automated testing since it allows us to deal with elements with dynamic values very well. The various ways to write XPath help the developer improve web application tests, which in turn enhances the user experience of the software they are developing.