Xpath in selenium : different functions and axes

Share:
In previous article we have discussed how to find xpaths in selenium. (To read it click here). Finding Xpaths of the element for web is easy but some times when you  can't find it normal way than it becomes difficult. For such xpaths you need to use different functions and axes. 


Useful Functions for finding xpath in selenium

There are different inbuilt functions which will help you to find your xpaths easy way. It will also optimize your xpath.  So let's go one by one. 

1. text() - 
Whenever you want to locate element with text, you have to use text(). We already used that in our previous post finding xpath in selenium.
For example 
different xpath Functions

As we learned, to find xpath of radio button, we need to find text associated with that radio button. So we need to find the xpath of radio button first. For that we can directly use text ().

//label[text()='Female']

2. Contains()
This function can be used in xpath when the values of attribute keeps on changing or not fixed or too long. 

For example lets say ID of element is something like "random.fb.btn.username", now that username is keeps on changing (this is just an example). then you can locate it like 

//*[contains(@id,'fb.btn')]

3.AND - OR

These work as Boolean function only. When you want to make two identifiers compulsory you can use AND, when you can find multiple ways to find xpath and all ways are correct than you can use OR. you can also use symbols & for AND and | for OR. 
For Example
//[contains(@id,'fb.btn')AND text()='Female' ]
This will find an element having id conatining "fb.btn" and text = "female". 

4. Starts-with function:

It can be used same way like "contains".  In contain it chackes the value with the string and  if any part of string matches given value than it is fine. Starts-with sees if string is starting with given value or not. 
//*[starts-with(@id,'fb.btn')]
So in this xpath id should start with "fb.btn" than only it will accept. 

Ueful Axes for finding xpath in selenium

These Xpath Axes are very useful to make complex xpaths simple. With this axes xpath becomes easy to understand . Here are the some useful xpath axes for selenium. 



AxisName
Result
ancestor
Selects all ancestors (parent, grandparent, etc.) of the current node
ancestor-or-self
Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself
attribute
Selects all attributes of the current node
child
Selects all children of the current node
descendant
Selects all descendants (children, grandchildren, etc.) of the current node
descendant-or-self
Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself
following
Selects everything in the document after the closing tag of the current node
following-sibling
Selects all siblings after the current node
namespace
Selects all namespace nodes of the current node
parent
Selects the parent of the current node
preceding
Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes
preceding-sibling
Selects all siblings before the current node
self
Selects the current node
This axis are very useful. Always keep in mind while creating your xpaths. Again in previous post finding radio button of facebook we used one axis and it made it simple. remembered ? (not, then To read it click here).

Keep following this blog. Subscribe using mail to get articles in your email box. Follow on social media as well.