Header Ads

  • Breaking News

    Xpath in selenium : different functions and axes

    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 ().


    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 


    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. 
    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. 

    Selects all ancestors (parent, grandparent, etc.) of the current node
    Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself
    Selects all attributes of the current node
    Selects all children of the current node
    Selects all descendants (children, grandchildren, etc.) of the current node
    Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself
    Selects everything in the document after the closing tag of the current node
    Selects all siblings after the current node
    Selects all namespace nodes of the current node
    Selects the parent of the current node
    Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes
    Selects all siblings before the current node
    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.

    Post Top Ad

    Post Bottom Ad