A Comprehensive Guide To Deal With XPath In Selenium
Published: 2021-01-27 06:39:25

---
title: A Comprehensive Guide To Deal With XPath In Selenium
published: true
description: Selenium has always been a first choice for automation developers to automate their web application. 
tags: 
//cover_image: https://direct_url_to_image.jpg
---
<img src="https://www.pcloudy.com/wp-content/uploads/2021/01/xpath-in-selenium-4.png">
<a href="https://www.pcloudy.com/selenium-for-mobile-app-testing/">Selenium</a> has always been a first choice for automation developers to automate their web application. Locating dynamic web elements have historically been dolorous for automation testers which brings the scope of XPath in picture.

Xpath is one of the most preferable locator provided by Selenium. Locators are Selenium commands used to locate a GUI web element present in the web page Document Object Model(DOM) structure. Below are few locators supported by Selenium to locate web elements:

ID
XPath
Name
Class Name
CSS Selector
Linktext
Partial Linktext
Tag Name
DOM Locator

Though ID and CSS locators are said to be the best and fast locators. However, it is sometimes difficult to locate a web element even with ID, Name and CSS; in such case different methods of XPath in selenium play a vital role to locate the dynamic element.

In this article, we would be more focusing on how to deal with dynamic XPath’s. Dynamic XPath are also known as Custom XPath. Before moving ahead, let’s have a quick look at the definition of XPath and different types of XPath in selenium.

<b>What is XPath in Selenium?</b>
XPath is a standard strategy supported by Selenium to locate web elements. It provides the path expression to navigate through elements and attributes in an XML document. XPath is defined as XML Path that helps to query the XML documents.

General syntax of XPath: //Tagname[@AttibuteName = ‘value’]

The above syntax defines four components:

Double forward slash(//) – Denote the current node
Tagname – Defines the tagname for the element to be located
AttributeName – Defines an attribute of the tag defined in the starting of the XPath
Value – Represents the value of an attribute defined
Types of XPath in Selenium
Absolute XPath: It provides the path expression from root node to the desired element node. The drawback of this XPath type is, if there is a change of any node/tag between the defined XPath expression, the whole XPath will fail to find an element. Absolute XPath always begins with a single forward slash i.e. ‘/’.
Example: /html/body/div[1]/header/div/div[1]/div[3]/div/form/div[3]/div[1]/input

<b>Relative XPath:</b> It provides the path being generated from the middle of the HTML DOM structure. It doesn’t need to start from a root node. It is said to be more reliable with a less chance of a script to be broken. Relative XPath begins with a double forward slash(//).
Example: //*[@id=”twotabsearchtextbox”]

<b>Various methods for handling XPath</b>
Though the browser allows us to inspect web elements and get XPath from DOM directly, but still as an automation tester, we must know how to create our own dynamic XPath. Let’s have a look at various methods for creating dynamic XPath in Selenium.

Note: Open the DOM of the web page by directing to inspect elements and press CTRL + F to open the DOM search field and try to create XPath on the same search field. If your created XPath is correct then the particular tag or element to be located would get yellow highlighted.

<b>Basic XPath –</b>
<img src="https://lh3.googleusercontent.com/NE8xyzMsHHGnOY6cXZ5IOPdjuyYI_SQi0JwC9MY6u8A62lTWEj7p8ep0g6LjHyZNYijBsKw9UJn1nEk3M4O6HzDWBP65Ilr_YJJ_DZtEJ6q5_MoTgj2R5umB4CyIFmt8GC2XH0iR">
Open Image in new tab for better view of DOM

This is one of the straightforward methods to create XPath. In the above screenshot the light blue highlighted tag of DOM represents the search field of Amazon web application. To locate this search field, below are few valid XPath that can be used:

//input[@id=’twotabsearchtextbox’]
//input[@name=’field-keywords’]
//*[@id=’twotabsearchtextbox’]
In the first XPath, we have used the “id” attribute and in second XPath, we have used “name” as an attribute. These types of XPath are very common and follow the exact syntactic approach defined above. In the third XPath, we have used asterisk(*) that would search id=’twotabsearchtextbox’ in any HTML tag present in DOM structure. Using of asterisk(*) is only recommended when you use ID as an attribute since ID is always unique.

<b>2) Using multiple attributes with ‘and’ & ‘or’ –</b>

This method is just an extension of the previous method with the use of logic operators. You might find a case where different HTML tags have similar attributes with similar values. Using such attributes in your XPath might throw an error or can take you to a different web element than expected. For such cases, you can use multiple attributes for forming a single XPath that satisfies all conditions.

Let’s refer to the same above screenshot for locating Amazon search field with following XPath:

//*[@class=’nav-input’ and @name=’field-keywords’]
//*[@class=’nav-input’ or @name=’field-keywords’]
If you see the DOM of <a href="https://amazon.com/">https://amazon.com</a> web page, you would see that the class=’nav-input’ is used multiple times in different HTML tags. So, when we use the “name” attribute along with “class” attribute using “and” keyword, it would point to the correct element that is the Amazon search field.

However, using ‘or’ keyword in the same expression as in the second XPath, would lead you to two different elements as shown in the below screenshot(showing “1 of 2” in the search field means 2 elements available for the same XPath). “OR” keyword would point to number of elements having attributes class=’nav-input’ or name=’field-keywords’
<img src="https://lh6.googleusercontent.com/HV8T6rIwuo3hwVah0XTZtrH_CoKUVUNfLL7oLhrQrEf_IquhYqjM0h2JBeUkkZQTQjoGX3b_NvBZjJjV5YGb5ds9a54c9JjIEGAahweIAdlQ9XA9I5-yxzU6ZsNAH3E_qy1TBzKy">

Contains() – This method is basically used in XPath to locate an element using partial value of an attribute. It is most preferable in cases where attribute values are dynamic in nature. Suppose you have an attribute in DOM whose value is “tata-007” and the last 3 digits are very dynamic and changes every time the page is refreshed. In such a case, we can only use the partial value such as “tata” to locate an element.
For ex: In the below screenshot of <a href="https://apple.com">https://apple.com</a>, we need to locate a top-bar element “Mac”. Though we have a class=’ac-gn-link ac-gn-link-mac’ given in DOM for element Mac, but still we can use only partial value of class attribute in order to locate.

//a[contains(@class,’link-mac’)]
//a[contains(@class,’link-ipad’)]
<img src="https://lh4.googleusercontent.com/QGZffnM9L_1IdWGQxWk1W-ARbURWFwdUw2A_wl6npjE6Z5g8rfoyrI71QOFNxdgyRp2WP3GAUJ_mr6hX9agb40gJOZqFnqtglOKcR7EsgCxP9Pg0irZf1zxWreiGcnDW-mhPmXPa">

In the first XPath, we have only used ‘link-mac’ which is a partial value of class=’ac-gn-link ac-gn-link-mac’. Similarly, to locate the iPad on the same web page, we have class=’ac-gn-link ac-gn-link-ipad’ in our DOM, however to use only a partial value, in the second XPath, we have just used ‘link-ipad’.

<b>4) Text()</b> – This approach can be used when we need to locate an element via text. We can use the text of an element given for that particular element HTML tag. This text can be either button name or label name or any other text visible on a web page.

Let’s refer to the above screenshot as an example to locate “Mac” on top-bar, given the DOM structure as :

<span class=”ac-gn-link-text”>Mac</span>

<b>following would be the XPath:</b>

//span[text()=’Mac’]
//span[contains(text(),’Ma’)]
The first XPath, we have only used the text of the “Mac” button on the top-bar with “span” as HTML tag name.

In the second XPath, we have just used the partial value of the “Mac” button that is “Ma”. As we have covered in the previous point about the contains method, it can be used to locate an element with only the partial text or partial attribute value.

<b>5) Index Value</b> – In this we specify the index value in our XPath of an element we wish to locate. This index value is similar to the indexing of an array. In the same screenshot above of https://apple.com we see that there is a top-bar of unordered lists which has an HTML tag <ul>. For this unordered list we have list items given in DOM with HTML tag <li>. Please refer to the above attached screenshot to see the full DOM structure, you would be able to checkout the list items such as Apple Logo, Mac, iPad, iPhone, Watch etc.

Following would be the valid XPath to locate these elements using their index value given in square brackets:

To locate “Mac” – //ul[@class=’ac-gn-list’]/li[2]
To locate “iPad” – //ul[@class=’ac-gn-list’]/li[3]
<b>6) Chained XPath</b> – As the name suggests, the Chained XPath is nothing but the combination of XPath merged in a chain. In the previous example of Index Value, suppose we have n number of list items and getting the index value for a particular item would be a challenging task. In such case, you can merge the different XPath having different HTML tags or/and different attributes and values.

Below XPath would help locating “Mac” and “iPad” with chained XPath created using  and  HTML tags given in DOM structure:
<ul><li>
To locate “Mac” – //ul[@class=’ac-gn-list’]//li[@class=’ac-gn-item ac-gn-item-menu ac-gn-mac’]</li>
<li>To locate “iPad” – //ul[@class=’ac-gn-list’]//li[@class=’ac-gn-item ac-gn-item-menu ac-gn-ipad’]</li></ul>
Now, we will have a look at XPath Axes that can be used for handling complex and dynamic XPath in Selenium by traversing through parent, child, siblings, Ancestor and Descendant of the context node. The context node is the node where the XPath is currently pointing at. Using XPath Axes is more useful when we do not have a direct way to locate the desired node and hence, complex traversing to our desired node with XPath Axes is required.

<b>7) Ancestor</b> – Ancestor can be used to select grandparent and parent node of the context node depending upon the ancestor HTML tag defined.

<img src="https://lh3.googleusercontent.com/iIlMJBA6V3AvHlRaSLJPjbmYMjdjZpDhlwgYBloqAZsTFdFvGwOJvxCTu7Pjo4cqSKbEUx11nxm4R-ESfxhw-sCTazjPdC2QzfnZKNz8K6u22oPq4v1hOESY5Ow9fcKOESaCRa07">
Suppose, we want to get only “div” ancestor of Create Account-First Name field, we would have the following XPath:

//input[@name=’firstname’]//ancestor::div
//input[@name=’firstname’]//ancestor::div[1]
The first XPath gives you all the HTML “div” tagname ancestor of name=’firstname’. However, the second XPath will only give the first ancestor of name=’firstname’ and that can be even a grandparent of grandparent depending on the DOM structure.

<b>8) Descendant</b> – Descendant is opposite to ancestor, it is used to select the children and sub-children of the context node. Here is the screenshot of <a href="https://facebook.com">https://facebook.com</a>

<img src="https://lh5.googleusercontent.com/900cRKM2xIN0lreyuc7_MSsoUN6C1UBuCmkXxx5Sr9nLCCAvD6y767XzKo6iNemNimYlgYlyGcUuDxOic2HMjQf2UiI5R-r0jmNqZ5sIRkmosnKbdhGpXAoRs58i5S1zlACYHiCI">
The XPath : “//div[@id=’reg_form_box’]//descendant::div” created according to the yellow highlighted context node would select all the children and sub-children of the context node.

<b>9) Parent</b> – Using the “parent” keyword in XPath would be helpful for selecting only the Parent node of the Context node. Let’s refer to the same instance of Ancestor method given above, we get the Parent node through its child node using the XPath:

//input[@name=’firstname’]//parent::div

In this case, the context node is the child node which is being used to select it’s Parent node.

<b>10) Child</b> – Using the “child” keyword in XPath would be helpful for selecting only the Child node of the Context node. Here the Context node would be the Parent node. Child will only select the immediate descendants of the context node, but doesn’t go deeper like the descendant method.

<b>11) Following</b> – This approach can be used to locate elements following the current/context node.

<img src="https://lh6.googleusercontent.com/eM3l-UC9xV0WqbwGe1q0OVdWnBdpZHTLh-6xj497qaiqrEIznAfN2dRYXKJgQw7OzWU4fpa2VNZXgEs5ZNWyFqPtCIPsP4H5ajNW7DBRvATdrdrugFOfRfP3bZj9IczXcUXWzrEB">
Here is the above screenshot of https://google.com where we will be locating the “I’m Feeling Lucky” button with the help of the “Google Search” button.

As we can see in the DOM, the element “I’m Feeling Lucky” button is following the “Google Search” button. Below would be the XPath for this:

//input[@name=’btnK’]//following::center//input[2]

<b>12) Preceding</b> – This approach can be used to locate elements that lie before the current/context node or in other words we can say, it can be used to locate the preceding element from the current/context node.

In the Google screenshot above, now let’s try to locate the “Google Search” button with the help of the “I’m Feeling Lucky” button. Below would be the XPath for this:

//input[@name=’btnI’]//preceding::center//input[1]

<b>13) Following-sibling</b> – Before proceeding with this method, you must be clear about the usage of the “Following” XPath axes method. In this case, all the following nodes of context nodes are selected depending on the following-sibling tagname no matter even if they are under or children of the context node, however, they would be having the same parent.

In the Google screenshot attached above, we would be using the div class attribute as the context node which has “Google Search” button and “I’m Feeling Lucky” button as their siblings, below is the valid XPath for the same:

//div[@class=’FPdoLc tfB0Bf’]//following-sibling::input

You might be getting confused between child and sibling now. You don’t have to get confused, keep in mind that all siblings are referred to as the child of the parent node. Following-sibling can also be used to traverse between different children of parent nodes.

<b>14) Preceding-sibling</b> – This is totally opposite to Following-sibling. In this case, you can traverse to sibling nodes before the context node. Let’s refer to the same screenshot of Google where we would be locating the “Google Search” button using it’s sibling “I’m Feeling Lucky” button. Here is the XPath for this:

//input[@name=’btnI’]//preceding-sibling::input[1]

Please make sure that you are giving the right index value otherwise you may get located on some other web element which may lead to failure of your automated test case.

Confused, which XPath method is more feasible?
When automating <a href="https://www.pcloudy.com/selenium-testing-for-effective-test-automation/">functional tests with Selenium</a>, it is often not clear which locator and what strategy is to be used to locate a desired element. Choosing a good locator plays a vital role in our automation framework as it defines how reliable, maintainable and durable our automation scripts would be.

Since we have covered different XPath methods above, you might be thinking which method would be more feasible. There are actually few set of things that need to be noticed before choosing any method like:

<b>Scope:</b> It is always important to not to be dependent on page design choices while choosing the locator method. Instead, the choices must be created on the basis of durability and reliability which actually defines the long term scope of each locator method. For instance, it is good to use relative XPath technique rather than absolute Xpath technique since the absolute path may break your script if any element structure gets changed from root.

<b>Technology:</b> It is said, not be dependent on UI implementation technology to automate UI test cases. For example, in case of an AngularJS application, try not to use angular attributes like ng-binding or ng-scope while creating a XPath with any of the methods.

<b>HTML Structure:</b> It is always good to fetch a locator path from the minimum area of HTML structure of a page. The more elements you add in a path to locate the desired element, the higher is the chance that even a minor change in UI would break your locator. Hence, trying to include a minimum number of elements or attributes to form a XPath is said to be a good practise.

<h2>Adopting SelectorsHub to generate XPath in Selenium</h2>
As the name suggests, SelectorsHub is the hub of selectors supported by Selenium. It is a browser extension and free to use plugin that auto-suggests XPath based on multiple XPath methods. It not only suggests XPath but also suggests all possible combinations of CSS selector.

<img src="https://lh4.googleusercontent.com/aIfX8ZN3M9p4L8eMaEDc2a8ui5z_RGVKEdVtw4VRWzzg2KPTZiKHhyeESky3QRloXM-Dew2tzFRGhdCKVST4akHhTh2Xu3hD-SBydASiXcS2qKpBf6iPMHJFnhDThWzIwIG2FX9e">
Going back to the history, before developing the SelectorsHub plugin, the creator of SelectorsHub did a short survey and got to know that 79% of automation developers love to create their own XPath instead of directly copying from browser dev tools. So basically, SelectorsHub was released with a motive to enhance XPath creating skills and selecting the robust locator among multiple combinations.

<b>Key features of SelectorsHub:</b>

Auto-Suggestion of XPath and CSS Selector
Secure and free to use plugin
Support for iFrame
Support for Shadow DOM elements
Support for SVG elements
To know more about this innovation, please have a look at this amazing <a href="https://www.youtube.com/watch?v=2gH5c97j2GU&feature=youtu.be">SelectorsHub tutorial</a>.