Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Accessing a Table from Xpath Locators or Making a Xpath Locators from a HTML code

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 814
    Comment on it

    For reading a table first we need to access elements within HTML tables. However, a web designer provides sometimes an Id or name attribute to a cell in the table. So we can't use the methods such as by.id,by.name or by.cssSelector(). So in this case, we can use one more element Locator by.xpath() method.

    XPath Syntax:

    Consider the HTML code below:-

     

    1. <html>
    2. <head>
    3. <title> misbah </title>
    4. </head>
    5. <body>
    6. <table border= 1>
    7. <tbody>
    8. <tr>
    9. <td> first cell </td>
    10. <td> second cell </td>
    11. </tr>
    12. <tr>
    13. <td> third cell </td>
    14. <td> fourth cell </td>
    15. </tr>
    16. </tbody>
    17. </table>
    18. </body>
    19. </html>
    20.  

    If we want the text of fourth cell of the table then we can use xpath method overe here. To make Xpath here some below rules we followed:

    Step 1 - Set the Parent Element :

    XPath locators in WebDriver always start with a double forward slash "//" and then followed by the parent element.

    eg: “//table”


    Step 2 - Add the child elements:

    <tbody> is the child of any table. So that all child elements in Xpath are placed to the right side of the parent separated by one forward slash “/”.

    eg: //table/tbody


    Step 3 - Add Predicates:

    In the above table the elements of two <tr> tags are the child of the <tbody> tags.

    //table/tbody/tr from this we can access only element of first <tr> and the elements of the tr is known as Siblings.

    According to above table if we want to access second row of the table then we provides the predicates like //table/tbody/tr[2].

    Predicates are the numbers in a pair of square brackets “[ ]” that distinguish a child element and it's siblings.

    eg: //table/tbody/tr[2]/td[2].

    According to above xpath here we are accessing second data of second row at above table.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: