Xpath usage for test automation

Xpath for selenium test automation

In selenium test automation elements can be identified using different ways, such as Id, name, class, tag name, link text, partial link text, xpath and css. If the html elements have attributes like Id and Name, it is suggested to use those attributes to identify the elements. The identification is faster using the attributes Id, Name. But with the applications that are generating dynamic content/pages you may not see the attributes like Id or Name. In such cases we need to depend on the other identifiers available.

The identification of elements with xpath or css is a bit slower than it is with id or Name. But Xpath and Css are super powerful element identification mechanisms with dynamically generated web pages. You may refer Amazon or Kiwi or any other eCommerce web applications for dynamically generated web pages.

Okay, with out having any delay lets get to the point.

xpath uses dom of the html page to traverse. So we use html tags to locate the required element. Before going in detail please go through the basic points to remember
Node representation:
  • // represents from current node
  • / represents from root node
  • . represents current node
  • .. represents parent of current node
  • @ uses for attribute of the node/tag

The html piece of code that we use with our examples,


Usage of xpath to fetch different elements:

I may refer elements or tags, both are same. lets see some examples,
/div
gets us the root `div` element
//div
gets all the elements with div tag
div/fieldset
gets all the filedset elements that are children to div
fieldset//div
gets all the div tags (child) under fieldset tag no matter where those tags present (it can be immediate child, grand child,…)
/div/fieldset/div/input
indexed xpath : /div/fieldset/div/input[1]
This first xpath gets all the input tags that the xpath evaluates/matches. But the second one gives only one element because we gave the index 1. But if the options are rearranged in that case it can still gets the element, but it may not be the required element in case of indexed xpath.
Okay lets try another way,
//input[text()=‘name’]
The above xpath can get all the input tags which have the text equal to `name`.
If you are not sure about the text, but you know the partial text then you can use,
//input[contains(text(), ‘name’)] //*[contains(text(), ‘name’)]
we can use `*` instead of `input` (tag name) i.e, `*` means any tag

Using `and` with xpath :
//input[@type=‘radio’ and contains(text(), ‘name’)]
xpath to Get elements with some attribute:
//input[@type]
gets the elements which has the `type` attribute

Okay, Lets know about other useful stuff we can use with xpath
we will be looking at following stuff that we use often with xpath,
  • parent : parent of the current node
  • self : current node
  • ancestor : parent, grand parent, great grant parent…
  • child : children nodes of current node
  • following-sibling : all siblings after the current node
  • preceding-sibling : all siblings before the current node

** Children of a same parent are called siblings.

Lets use another example to learn other tips,

usage of parent in xpath:
If you want to get the `div` tag which has the radio buttons name1, id1 as children.
//*[contains(text(), ‘name1’)]/parent::div

usage of following-sibling in xpath:
To all the input tags under input tag `name2`
//*[contains(text(), ‘name2’)]/following-sibling::input

usage of preceding-sibling in xpath:
To get all the input tags above input tag `name2` that have attribute row
//*[contains(text(), ‘name2’)]/preceding-sibling::input[@row]
To get all the tags above input tag `name2` that have attribute row
//*[contains(text(), ‘name2’)]/preceding-sibling::*[@row]

Here are some random examples of xpath
By.xpath(“.”) gives current node
By.xpath(“..”) gives parent node of the current node
By.xpath(“../..”) gives parent of parent node of the current node

I hope now you got some basic idea on how to use xpath for test automation. Please post a comment if you need any help. I am happy to help you. Thanks for reading. Happy learning.

Please post a comment incase if you find anything wrong. Thankyou.

Comments