App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide Quick XPath Locators Cheat Sheet

Quick XPath Locators Cheat Sheet

By Gurudatt S A, Community Contributor -

Browser Automation requires navigation through the elements of HTML / XML pages to automate its functions for website test automation. Frameworks like Selenium use XPath to locate the elements and perform the required functions for testing. This guide discusses various aspects of XPath and how to use it through this Selenium XPath cheat sheet.

What is XPath?

XPath stands for XML Path Language. XPath is mainly used in XSLT but is also popularly used for navigating through the DOM of any XML-like language document using XPathExpression. It is a type of Locator.

In test automation, XPathExpression in HTML Pages is used for navigating elements in an HTML page.

Types of XPath

An XPath expression can be written in two ways 

  1. Absolute
  2. Relative

To understand this better, let’s use the browserstack demo application as seen below.

SelectDropDown in Xpath

As an exercise, let’s create an XPath expression to identify the Select dropdown.

1. Absolute XPath

Absolute XPath is the way of identifying an element starting from the root of the DOM. The XPath expression starts with “/” symbol. 

The drawback of writing Absolute XPath is length and maintainability. If there is a removal or inclusion of new element in the DOM within the absolute XPath already written, then the XPath expression will break and has to be rewritten again

Below is the example of Absolute XPath for identifying Select dropdown.

Absolute Xpath

To obtain the Absolute XPath:

  1. Open the application in the Chrome browser and click on the F12 key on the keyboard to open Dev Tool
  2. Inspect the Element
  3. Right-click on the inspected element in the Elements tab and select Copy > Copy Full XPath

The Absolute XPath of an element looks like this:


2. Relative XPath

Relative XPath is the way of identifying an element by using its attributes for querying and can also be used for its nearest elements. Relative XPath doesn’t start with a root node; hence, this way of writing XPath is always reliable. The Path expression starts with “//”

Below would be the example of Relative XPath for identifying Select dropdown

Relative Xpath

The Relative XPath of an element looks like this


Different ways to Query an Element for XPath Locator

An element can be queried by using

  • Axes
  • Functions

1. Axes

There are 13 different axes for querying an element. Axis is used when we want to query for an element using its self attributes/ nearby elements, and hierarchical building relationships.

ancestorancestor is used when we want to check all the parent nodes from the context node up to root.
ancestor-or-selfancestor-or-self is used when we want to select all the parent nodes upto root including the context node
attributeattribute is used when we want to query for an element using its attributes like id, class, value etc..
childchild is used when we want to query for child element with parent reference
descendantdescendant is used when we want to query for all the child element and its children of a context node
descendant-or-selfdescendant-or-self is used when we to query for all the child elements and its children including context node
followingfollowing is used when we want to query for all the elements after the current context node(except the context node’s descendant)
following-siblingfollowing-sibling is used when we want to query for the sibling element which belongs to same parent as context node
parentparent is used when we want to query for the immediate parent of the current context node. This uses two dots “..”
precedingpreceding is used when we want to query for elements that are preceding to the current context node
preceding-siblingpreceding-sibling is used when we want to query for the sibling element preceding to the current context node
selfself is used when we first query for a set of elements and then refine further for the current context node. This axis uses a single dot “.”

2. Functions

XPath functions come in handy when we need to check the existence of elements or return elements based on their attributes, position, etc.

Below are the most useful functions which can be used on day to day basis:

boolean()This function accepts locator as parameter and it return true if the passed locator is found else returns false
count()This function accepts locator as parameter and returns the count of elements found
id()This function accepts id attribute of the element and returns the element if found
position()This function is used when we need to identify an element based on its position

Note: There are a total of 37 functions and a detailed list can be found here


We have seen the differences between Absolute and Relative XPath. As a tester or developer, it is best practice to avoid using Absolute XPath expression as it requires more maintenance and is relatively less reliable. And with the combination of Relative XPath and along with Axes and Functions, can be used to write powerful XPath expressions which are reliable and optimized.

By using XPathExpression with Selenium, you can automate various elements on the HTML pages for testing web applications.

Automated UI Testing Automation Testing Website Testing

Featured Articles

How to use XPath in Selenium? (using Text, Attributes, Logical Operators)

How to find element by XPath in Selenium with Example

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.