Complete Guide For Using XPath In Selenium With Examples

Posted by Sadhvi Singh | May 14, 2019
Selenium Locators • Selenium Tutorial •

36085 Views | 17 Min Read

Complete Guide For Using XPath In Selenium With Examples

Identifying element has always been the trickiest part and therefore require an accurate and correct approach. If you are unable to find elements by the easily available approaches like ID, class, name, link or tagname then XPath in Selenium can help rescue you. Locating dynamic elements have always been the pain area while you wish to automate the scripts, the only ray of hope to deal with such fiascos is XPath. In my current article, I will be digging into the steps of locating an element via XPath example in Selenium and its various ways. We will be looking into the below sections:

If you wish to look into other approaches of locating an element, you can view the below articles:

  1. How to locate element via ID
  2. How to locate element via Name
  3. How to locate element by Tagname
  4. How to locate element by CSS Selectors
  5. Different locators in Selenium WebDriver

So, What Is XPath In Selenium?

XPath known as the XML path is a language that helps to query the XML documents. It consists of expression for a path along with certain conditions to locate a particular element.

So let’s see how to write an XPath in Selenium. Below is the DOM structure for LambdaTest Registration Page:

Referenced screenshot of the page:

Referenced screenshot

LambdaTest is a cross browser testing cloud that offers a Selenium Grid of 2000+ real browsers & browser versions to help you deliver a seamless user experience. Click here to get started for free.

In order to visualize the above XML document, I have created the below flowchart.

flowchart

Here, I will write the XPath in Selenium from the root node which is the form tag in our case, then will locate the Full name field using div 2 child and the attribute value of the full name field. The XPath created in this case will be as below:

//form/div[@class=’ col-sm-12 google-sign-form’]/input[@name=’ name’]

In this case, I have referenced one attribute of the children and grandchildren in order to differentiate from the multiple children and grandchildren of each parent. From this we can derive the general syntax of XPath as follows:

//tagname[@attribute name= ‘value’]

Here

  1. // Denotes the current node
  2. Tagname : Define the tagname you are referencing to locate the element.
  3. Attribute Value : The attribute of the define tag through which you wish to narrow down the search.
  4. Value: Represents the value of any chosen attribute.

Nowadays, thorough use of multiple plugins and tools can help one find the XPath in Selenium with relative ease. The reason I don’t encourage people to do so, the XPath provided by these tools sometimes turn out to be brittle. Also, it may lead to people forgetting the basic concepts of creating XPath locators. Using firebug and Chrome Dev Tools one can also copy the required XPath in Selenium.

Types Of XPath In Selenium

There are two types of XPath

  1. Absolute XPath
  2. Relative XPath

Absolute XPath Example In Selenium

In case of absolute XPath in Selenium, the XPath expression is created using the selection from the root node. It starts with a single slash ‘/’ and traverses from the root to the whole DOM to reach to the desired element. The biggest disadvantage of using this as locating an element is, if during the course of development any changes made in the path, may lead to a failed XPath expression. For example:

/html/body/div[1]/section/div/div[2]/div/form/div[2]/input[3]

In the above XPath example in Selenium, if any tag name like section or one of the div’s changes the whole XPath would become invalid leading to script failure.

Relative XPath Example In Selenium

In case of relative XPath in Selenium, the XPath expression is generated from the middle of the DOM structure. It is represented by a double slash ‘//’ denoting the current node. In this case the search will start from the mentioned tagname and string value. It is more compact, easy to use and less prone to been broken. For example:

//input[@name=’email’]

In above XPath example in Selenium, we are searching from the current node with tagname input having attribute as name with value as email.

Below is a code snippet highlighting the XPath written using relative XPath for the register page of LambdaTest.

Writing Complex & Dynamic XPath In Selenium Through Various Methods

Using Basic XPath

This is the common and syntactical approach of writing the XPath in Selenium which is the combination of a tagname and attribute value. Here are few basic XPath examples in Selenium:

  • Xpath=//input [@name=’password’]
  • Xpath=//a [@href= ‘https://www.lambdatest.com/’]
  • Xpath=//*[@id=’email_01’]
  • Xpath=//input[name=’email’][@placeholder=’Work Email’]

The first two examples in the basic XPath list seems self-explanatory. In it we have used tags as input and anchor tag with their corresponding attribute value. The last two examples are just extended version of using XPath in Selenium. In the third example we have just excluded the HTML tag and represented it with an asterisk(*). In this case, the script will search in the DOM with any HTML tag having an ID attribute with value as ‘email_01’. In case of example 4, we have created the XPath using multiple attributes for the single HTML tag.

Web UI Testing with Python and LambdaTest

Using ‘OR’ & ‘AND’

As mentioned these logical expressions are used on the attributes condition. In case of OR any one of the conditions should be true or both, whereas in the case of AND, both the conditions should be fulfilled. For example, for the below DOM structure, XPath using AND and OR can be written as:

  • Xpath= //input[@type= ‘email’ or @name= ‘email’]
  • Xpath= //input[@type= ‘email’ and @name= ‘email’]

In case of OR if any of the attribute value matches, the element will be located, whereas in case of AND both the attribute value should match and met, then only the element will be located. The above defined ways have been inculcated in the code snippet below, where we are trying to input values on the LambdaTest register page.

Below is the console error showcasing as the last element not found as conditions were not met.

 error showcasing as the last element not found

Please Note: Both ‘and’ and ‘or’ should be case sensitive. In case you tend to use, ‘OR’ or ‘AND’, you will get an error in console stating invalid xpath expression

Using Contains()

This approach in creating XPath in Selenium comes handy when we have part of attribute values that changes dynamically. For example lets say the ID for the login field which is for instance email_01, have the ending number which keeps changing every time the page is loaded. In this case using contains helps us locate element with constant names like in this case ‘email’. Example for creating XPath with contains for the below DOM structure is:

  • Xpath=//*[contains (@placeholder, ‘Organization)]
  • Xpath= //input[contains (@name, ‘organization)]
  • Xpath=//*[contains(@class, ‘sign-up-input)]

Here in the above XPath example for Selenium we have used, attributes like placeholder, name etc and consider partial values to identify elements using contains keywords. Referenced below code snippet highlighting the usage of above XPath creation, in this we are clicking on the ‘Start testing’ button on LambdaTest homepage. Referenced screenshot:

LambdaTest homepage


Using Starts-With()

This is similar to the above contains method, the only difference is the comparison value here starts with an initial string value. This is useful when partly values changes for a given attribute. Like in the above XPath example, the email value changes for the latter values. Here, one can use starts-with approach to identify the element. Below XPath examples for Selenium, highlight the usage of starts-with for the below DOM structure:

  • Xpath=//input[starts-with(@placeholder, ‘Organization)]
  • Xpath= =//input[starts-with(@name, ‘organization)]

Here, in the example above we have used two attributes with the starts-with Keyword. The script will find for the tag name and matches with the attribute value that starts with ‘Organization’. Referenced code snippet highlighting the usage of Starts-With keyword while locating element via XPath in Selenium. We will use the same example as above, the only difference is, we will locate an element using starts-with.

Using Text()

This keyword is used to create expressions for XPath in Selenium when we have a text defined in an HTML tag and we wish to identify element via text. This comes really handy when the other attribute values change dynamically with no substantial part of the attribute value that can be used via Starts-with or Contains. Below is an XPath example in Selenium, highlighting the usage of text for the following DOM structure:

  • Xpath= //button[text()=’ Signup for Free’]
  • Xpath=//button[contains(text(),’ Signup’ )]

In the above mentioned examples, we have use the text on the button to identify the element. Here two instances of the examples are used, one where the text is exact matched, whereas the other where the text is matched partially using contains keyword. Referenced snippet below highlighting the usage of the text keyword. In this example, we will be clicking on one of the blogs on the LambdaTest blog page. Referenced screenshot below:

 LambdaTest blog

Using Index

This approach comes in use when you wish to specify a given tag name in terms of index value you wish to locate too. For instance, consider a DOM having multiple input tags for each different field value and you wish to input text into the 4th field. In this case you can use the index to switch to the given tag name. For example:

For the given DOM structure, I wish to locate the second input tag field:

  • Xpath= //div[@class= ’ col-sm-12 google-sign-form’]/input[2]

In the above example, the locator with ‘Full name’ as a field will be selected. The first input tag will be ignored whereas the other tag will be considered owing to the index mentioned. This type of approach comes in handy when dealing with data in tables. For example, when you have multiple rows, you can reference the desired row using an index. Reference code snippet using an index for a tabular form of data for the registration page of LambdaTest, where through the form class we are navigating to the ‘full name’ field using index.

lambdatest login page

Using Chained XPath In Selenium

As the name signifies, we can use multiple XPath expressions and chained them. For example, in the below DOM structure referencing the LambdaTest homepage links:

Complete Guide For Using XPath In Selenium With Examples

Here, we will try to navigate to the ‘Login link’. The syntax to achieve it is.

  • Xpath=//ul[@class=’nav navbar-nav navbar-right’]//li[@class=’sign-in’]

In the above XPath example in Selenium, I have located the parent class for the link tags and then navigated to the child using chaining for the ‘login’ link. The above example has been achieved using the below code snippet.

Console Output

XPath example in Selenium

XPath axes

XPath axes comes in handy when the exact element tagname or its attribute value are dynamic and cannot be used to locate an element. In this case locating element after traversing through child/sibling or parent will be easy approach. Some of the widely used XPath axes are:

1.   Following: This XPath axes helps to locate element following the current node. Mentioned below is the DOM structure of the LambdaTest Registration Page.

Referenced screenshot highlighting the two input elements, wherein we will try to locate the element ‘Full name’ field through the element ‘Organization’ field

Complete Guide For Using XPath In Selenium With Examples

  • Xpath=//input[@name=’ organization_name’]//following::input[1]
  • Xpath= //input[@name=’ organization_name’]//following::input

In the above-mentioned examples, the first one will select the input following the organization element, whereas in the second example, it will select all elements following the organization element having input tag. These are useful when we intent to locate element in a table or when we do not have any whereabouts of the following element from the current node. Mentioned below code snippet displays the elements located via following. In this example, we will use the Lambatest homepage, where we will traverse through the menu header options using the single header class. Referenced screenshot below:

Complete Guide For Using XPath In Selenium With Examples

2.   Following sibling: This is one concept that people tend to get confused with. All you get to clear yourself with this concept, is siblings. In case of following all nodes under the current node are the targeted ones irrespective they are under(children) the context node or not but follows below the context node. In case of following siblings, all following nodes of the context node, that shares the same parent, are applicable. In this case all siblings are referred to as children of the parent node. So if you are referencing one of the children and wish to navigate to other children of the same parent that follows it, following sibling does the business.

For example, using the LambdaTest homepage links, in the DOM structure below, one of the children has been referenced, and from it, we will navigate to its siblings.

In this case, the parent is,its children are the different li, here we will locate the sign in link and from their locate the login using following sibling

  • Xpath= //li[@class=’sign-in’]//following-sibling::li

Below is a code snippet incorporating the XPath example in Selenium.

Console Output

Complete Guide For Using XPath In Selenium With Examples

3.  Preceding:This method helps in locating element before the current node, as in the preceding element from the current node with XPath in Selenium. Below is a demonstration of the DOM structure.

Referenced screenshot below:

Complete Guide For Using XPath In Selenium With Examples

  • Xpath=//input[@name=’password’]//preceding::input[1]
  • Xpath=//input[@name=’password’]//preceding::input

In the above-mentioned example, the first one will locate element with the field as email whereas the other one will locate all elements before the current node i.e. password field. This is also useful in locating elements that cannot be located by any means and can be traversed through. For example, in the case of cross browser testing, sometimes few elements cannot be recognized on IE browsers, legacy browser versions. In this case traversing to these elements using precedence or following could be helpful. Referenced example below with code snippet, highlighting how using the blog link on the LambdaTest homepage, we will circulate through the Automation tab.

Complete Guide For Using XPath In Selenium With Examples

4.  Preceding-Sibling: This is a concept similar to following sibling, the only difference in functionality is that of preceding. So, in this case, you can switch among the siblings, but in this, you will switch from the context node been a child and move to the preceding node, you wish to locate. Both the child will share the same parent. Using the same example as mentioned in the following sibling, we will now move from the sign-up link to the login link using preceding-sibling. The syntax for the same is

  • Xpath= //li[@class=’login’]//preceding-sibling::li[1]

Note: In case you do not specify the index of the sibling you wish to navigate it may choose to select any. For specific selection, you need to mention the index. In case of a single sibling from the context node, you need not specify the index. Below is the code snippet for the same XPath example in Selenium.

Console Output

output

5.  Child: As the name specified this approach is used when we intent to locate all child elements of the current node. A basic use case of this approach could be when we wish to circulate through all the data in a table through the rows, in this case we can find all the child of a particular table. For example, using the below referenced DOM structure, we can create an XPath in Selenium as follows:

Referenced screenshot of the same.

Complete Guide For Using XPath In Selenium With Examples

  • Xpath= //div[@class=’ col-sm-12 google-sign-form’]/child::input
  • Xpath= //div[@class=’ col-sm-12 google-sign-form’]/child::input[1]

In the above-mentioned example, the first will locate all the input fields of the div for which are organization field, name field, email field, password field and phone number field, where as the other is used to locate element with the organization field only.

Below is the code snippet highlighting the use of child axes on the same. We are using the same register example of LambdaTest register page.

6.  Parent: This method is used to select the parent node of the current node. For example, for the below referenced DOM structure, the parent node is located through XPath.

Referenced screenshot:

Complete Guide For Using XPath In Selenium With Examples

  • Xpath= //input[@name=’email’]//parent::div

In the above example, the parent element is located through its child element. From the email field we are traversing to its parent node. Please note in case of child XPath, only the immediate children of the current node are considered and not the grandchildren. In case if the current node is the root node, then its parent will be empty and hence it will have no parent aces. Below is the code snippet for the same:

Console Output

Complete Guide For Using XPath In Selenium With Examples

7.  Descendants: This approach is used to locate element via XPath for all the children and sub children of the current node. For the below DOM structure, the descendants for XPath in Selenium are located as:

Referenced Screenshot:

  • Xpath= //div[@class=’ col-lg-3 col-md-4 col-sm-6 sign-form’]//descendant:: input

In the example above all the elements like email field, password field and button will be selected. One can also select one of the mentioned fields using index. Below is a code snippet highlighting the use of it.

Console Output

Complete Guide For Using XPath In Selenium With Examples

8.  Ancestors: In this method the context node, parent or its grandparents are selected via the Ancestors axes. For example, for the below DOM structure the ancestor will be defined as:

Referenced screenshot:
output

  • Xpath= //input[@name=’email]//ancestor::div[1]
  • Xpath= //input[@name=’email]//ancestor::form

In the example above, the first one will point to the parent node of the login fields where the other will point to its grandparent node which is the tag form in this case. Below code snippet highlighting the use of the same.

Console Output

 Console Output

Conclusion

XPath in Selenium helps an individual find out solutions in locating elements when the standard processes do not work. When to use which is important when it comes to different complexities of the DOM structure and the functional needs. As defined earlier in my article, cross browser testing could be one of the areas where you can explore the creation of XPath in Selenium, due to different browser behaviors towards DOM element. Make sure whichever method you opt in case of XPath are less prone to failure and make the scripts more robust, neater and easier to maintain. Cheers!

Written by Sadhvi Singh

Sadhvi Singh is a QA Manager. In 7 years of her professional journey, she has worked on multiple domains and testing techniques like Automation testing, Database testing, API testing, Manual testing, and Security testing. With multiple tools exposure added to her experience, she has skilled herself further through her two major accolades at International level through ISTQB with foundation and advanced levels. She has a technical blog named as qavibes.blogspot.com where she caters to all the challenges offered in the day to day journey of the quality assurance aspirants. In her spare time, she also works as a technical trainer and mentor to many budding QA professionals.

Related Articles

One thought on “Complete Guide For Using XPath In Selenium With Examples

Leave a Reply

Your email address will not be published. Required fields are marked *