Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Xpath in Selenium

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 96
    Comment on it

    The query language which is used to locate elements in XML document is "xpath".
    XML is the context in which xpath exist. One or more elements exists inside the XML document. Every element must have a start and end tag if it contains some content (text or other element).

    <Element> text </Element>

    Below is the sample XML:

    <Hollywood>
    <Film Title="XML Tutorial" Director="Selenium Easy">
    Text between the tags
    </Film>
    </Hollywood>

    Here, 1. "Title" and "Director" are the attributes of the tag "Film".
             2. "Text between the tags" is the content of the tag "Film".


    There are two types of Xpath:
    1. Absolute XPath
    2. Relative XPath

    1. Absolute XPath: Absolute xpath starts with the root node or a forward slash. For eg: html/body/div/tr/a
    Advantage: If we want to locate web elements fast then we can go with the Absolute xpath.
    Drawback: If any tag is removed or added in the HTML then this xpath will be changed and is of no use.

    2. Relative XPath: If the path starts with the node that we have selected then it is a relative xpath. It starts with double slash "//".  For eg: //form/tr/a
    Advantage: It is not required to mention long xpath. You can start from middle.
    Drawback: Here we specify partial xpath so it will take more time in identifying elements.

    We should prefer Relative xpath over Absolute xpath because in future if any web element is added/removed  then it will affect the absolute xpath but not Relative xpath. For eg:
    if the absolute xpath is "html/body/div/tr/a" and a new web element "<a>" tag is placed inside a new tag "<td>" then the new absolute xpath will be "html/body/div/tr/td/a" but the Relative xpath remains the same. So it is suggested to use Relative xpaths in Automation.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: