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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 514
    Comment on it

    Xpath functions: Sometimes we need to create generic xpath to locate a web element. We can create generic xpath using the xpath functions. Some of the commonly used xpath functions are as follows:

    1. contains()
    2. starts-with()
    3. last()
    4. text()
    5. sibling

    1. Contains:  This method is used to locate the elements that matches a particular text.

    For example:

    <a href="www.google.com" class="demoClass"> Google </a>

    Here, if we want to locate this anchor tag then we can use “contains” method as follows :
    //a[contains(@class, ’demoClass’)], which means "find 'a'(anchor) web element that has attribute 'class' value equal to ‘demoClass’ ”

    Or,  we can use other attribute to find this "a" web element. Like, //a[contains(@href,'google')], which means "find 'a' web element that has attribute 'href' value equal to 'google'"

     

    2. Starts-with :  This method is used to locate a web element that has attribute value starting with the some text.
    For eg:

    <a href="www.google.com" class="demoClass"> Google </a>

    Here, we can find this 'a' web element using starts-with() as follows:
    //a[starts-with(@class,'demo')], which means "find 'a' web element whose 'class' attribute starts with 'demo' "

     

    3. last() :  This method will select the last element amongst the available elements. For eg:

    <html>
    <body>
    <input type='checkbox' name='chk'>one
    <br><input type='checkbox' name='chk'>two
    <br><input type='checkbox' name='chk'>three
    <br><input type='checkbox' name='chk'>four
    <br><input type='checkbox' name='chk'>five
    <br><input type='checkbox' name='chk'>six
    </body>
    </html>

    Here, there are six checkboxes so last() will select the last checkbox. We can select the last checkbox using the following xpath: //input[contains(@type, 'checkbox')][last()]

     

    4. text() :  This method is used when we want to find a web element using exact match. For eg:
    <a href="www.google.com" class="demoClass"> Google </a>
    Here, we can use text() to find this web element: //a[text()='Google'], which means "find this web element whose text is 'Google'".

    5. sibling :  This method is further divided into 2 category:
    a. following-sibling
    b. Preceding-sibling

    We will cover this with the help of the example mentioned below:
     

    <html>
        <body>
            <table>
                <tr>
                    <th> Name </th>
                    <th> Course </th>
                </tr>
                <tr>
                    <td> ABC </td>
                    <td> MCA </td>
                </tr>
                <tr>
                    <td> XYZ </td>
                    <td> B.tech </td>
                </tr>
            </table>
        </body>
    </html>

    Here, 1. <th> Name </th> and <th> Course </th> are siblings.
              2. <td> ABC </td> and <td> MCA </td> are siblings.
             3. <td> XYZ </td> and <td> B.tech </td> are siblings.
             4. All the 'trs' are siblings.
          
    # Case1: If we want to find the course of the student named "ABC" then for this, we need to first locate the student "ABC". xpath for the condition is as follows:
    "//td[contains(text(),'ABC')]/following-sibling::td[1]"

    Explanation of the above xpath: First break the xpath in smaller sections then it will be easy to understand
    a. //td : this will find all the 'tds' in the web page.
    b. //td[contains(text(),'ABC')] : this will find the "td" whose text contains the value "ABC"
    c. //td[contains(text(),'ABC')]/following-sibling : this will find all the following siblings of the "ABC"
    d. //td[contains(text(),'ABC')]/following-sibling::td[1] : this will find the first following sibling of the node "ABC".

    #Case2:  If we want to find the name of the student whose course is "B.tech" then xpath will be:
    "//td[contains (text(),'B.tech')]/preceding-sibling::td[1]"

    Explanation of the xpath:
    a. //td : this will find all the 'tds' in the web page.
    b. //td[contains (text(),'B.tech')] : this will find the "td" whose text contains the value "B.tech"
    c. //td[contains (text(),'B.tech')]/preceding-sibling : this will find all the preceding siblings of the "B.tech"
    d. //td[contains (text(),'B.tech')]/preceding-sibling::td[1] : this will find the first preceding sibling of the node "B.tech".

 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: