Skip to main content

XPath for Selenium WebDriver

How to create Xpath locators...?

What are the different types of XPath?

There are two kinds of XPath expressions-

  1. Absolute XPath - The XPath expressions created using absolute XPaths begins the selection from the root node. These expressions either begin with the '/' or the root node and traverse the whole DOM to reach the element.
  2. Relative XPath - The relative XPath expressions are a lot more compact and use forward double slashes '//'. These XPaths can select the elements at any location that matches the selection criteria and doesn't necessarily begin with the root node.
The ways of finding dynamic elements using XPath...


What is an XPath?

An XPath can be defined as a query language used for navigating through the XML documents in order to locate different elements. The basic syntax of an XPath expression is:- 
//tag[@attributeName='attributeValues']

the different elements in the Xpath expression syntax - tag, attribute and attributeValues


Many times in automation, we either don't have unique attributes of the elements that uniquely identify them or the elements are dynamically generated with the attribute's value not known beforehand. For cases like these, XPath provides different methods of locating elements like - using the text written over the elements; using element's index; using partially matching attribute value; by moving to the sibling, child or parent of an element which can be uniquely identified etc.

Using text()

Using text(), we can locate an element based on the text written over it e.g. XPath for the 'google search' button -
//*[text()='Google Search']   (we used '*' here to match any tag with the desired text)

Using contains()

The contains(), we can match even the values of the partially matching attributes. This is particularly helpful for locating dynamic values whose some part remains constant e.g. XPath for the outer div in the above image having id as 'sb_ifc0' can be located even with partial id-'sb' using contains() - //div[contains(@id,'sb')] 

Using the element's index

By providing the index position in the square brackets, we can move to the nth element satisfying the condition e.g. //div[@id='elementid']/input[4] will fetch the fourth input element inside the div element.

Using XPath axes

XPath axes help in locating complex web elements by traversing them through a sibling, child or parent of other elements which can be identified easily. Some of the widely used axes are-

  • child - To select the child nodes of the reference node. Syntax - XpathForReferenceNode/child::tag
  • parent - To select the parent node of the reference node. Syntax - XpathForReferenceNode/parent::tag
  • following - To select all the nodes that come after the reference node. Syntax - XpathForReferenceNode/following::tag
  • preceding - To select all the nodes that come before the reference node. Syntax - XpathForReferenceNode/preceding::tag
  • ancestor - To select all the ancestor elements before the reference node. Syntax - XpathForReferenceNode/ancestor::tag

Comments