Pratik Barjatiya
4 min readDec 6, 2018
Effective XPATH Checklist

The Importance of XPath in Web Automation: A Beginner’s Guide.

If the elements are not identified by the general locator strategies like id, class, name, etc. then XPath is used to find an web element.

# What is XPath
XPath is defined as XML path. It is a way for finding any element on the web page using XML path expression. XPath is used to find the location of any element on a webpage using HTML DOM structure. The basic format of XPath is explained below -

# Syntax for XPath

XPath contains the path of the element situated at the web page. Standard syntax for creating XPath is.

Xpath=//tagname[@attribute=’value’]

// => Select current node.
Tagname => Tagname of the particular node.
@ => Select attribute.
Attribute => Attribute name of the node.
Value => Value of the attribute.

# Types of Locators
ID — To find the element by ID of the element
Classname — To find the element by Classname of the element
Name — To find the element by name of the element
Link text — To find the element by text of the link
XPath — XPath required for finding the dynamic element and traverse between various elements of the web page
CSS path — CSS path also locates elements having no name, class or ID.

# Types of X-path

  1. Absolute XPath — It is the direct way to find the element, but the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed. The key characteristic of XPath is that it begins with the single forward slash(/) ,which means you can select the element from the root node.
  2. Relative XPath — For Relative Xpath the path starts from the middle of the HTML DOM structure. It starts with the double forward slash (//), which means it can search the element anywhere at the webpage.You can start from the middle of the HTML DOM structure and no need to write long xpath.

# XPath Axes

XPath axes search different nodes in XML document from current context node. XPath Axes are the methods used to find dynamic elements, which otherwise not possible by normal XPath method having no ID , Classname, Name, etc.

Axes methods are used to find those elements, which dynamically change on refresh or any other operations. There are few axes methods commonly used in Browser Automation like child, parent, ancestor, sibling, preceding, self, etc.

# Handling complex & Dynamic elements using XPATH

  1. Basic XPath: XPath expression select nodes or list of nodes on the basis of attributes like ID , Name, Classname, etc. from the XML document as illustrated below.

Xpath=//input[@name=’testName’]

Some more basic xpath expressions:

Xpath=//input[@type=’text’]
Xpath= //label[@id=’testId']
Xpath= //input[@value=’testValue’]
Xpath=//*[@class=’testClass’]
Xpath=//a[@href=’http://demo.test.com/']
Xpath= //img[@src=’//cdn.test.com/images/home/test.png’]

2. Contains(): Contains() is a method used in XPath expression. It is used when the value of any attribute changes dynamically, for example, login information.The contain feature has an ability to find the element with partial text.

In this approach, we try to identify the element by just using partial text value of the attribute. In the below XPath expression partial value ‘sub’ is used in place of submit button.

Xpath=//*[contains(@type,’sub’)]

Xpath=//*[contains(@name,’btn’)]

In the above expression, we have taken the ‘name’ as an attribute and ‘btn’ as an partial value for btnLogin.

3. Using OR & AND: In OR expression, two conditions are used, whether 1st condition OR 2nd condition should be true. It is also applicable if any one condition is true or maybe both. Means any one condition should be true to find the element.

In the below XPath expression, it identifies the elements whose single or both conditions are true.

Xpath=//*[@type=’submit’ OR @name=’btnReset’]

In AND expression, two conditions are used, both conditions should be true to find the element. It fails to find element if any one condition is false.

Xpath=//input[@type=’submit’ and @name=’btnLogin’]

4. Start-with function: Start-with function finds the element whose attribute value changes on refresh or any operation on the webpage. In this expression, match the starting text of the attribute is used to find the element whose attribute changes dynamically. You can also find the element whose attribute value is static (not changes).

For example -: Suppose the ID of particular element changes dynamically like: Id=” message12"

Id=” message345"

Id=” message8769" and so on.. but the initial text is same. In this case, we use Start-with expression.

In the below expression, there are two elements with an id starting “message”. In below example, XPath finds those element whose ‘ID’ starting with ‘message’.

Xpath=//label[starts-with(@id,’message’)]

5. Text(): In this expression, with text function, we find the element with exact text match as shown below. In our case, we find the element with text “UserID”.

Xpath=//td[text()=’UserID’]

Pratik Barjatiya
Pratik Barjatiya

Written by Pratik Barjatiya

Data Engineer | Big Data Analytics | Data Science Practitioner | MLE | Disciplined Investor | Fitness & Traveller

Responses (1)