Search code examples
seleniumxpathrobotframework

How to get xpath from "Page Should Contain" in a web page using RobotFramework and Selenium


I'm writing test cases for a webpage that has a table as the main focus of the page. I'm checking that the page contains a new entry via the Page Should Contain fucntion, I then want to find the xpath for this element. I am doing this because each entry is added to the end of the table so I cannot set a static xpath for it because I don't know how many entries will be added between each run of this particular test.

Thanks in advance.

<table class="fw-table">
<thead>
   <tr class="affix-header">
      <th class="columnwidth-description">Email</th>
      <th>Account</th>
      <th>Signed up</th>
      <th>Number of invalid attempts</th>
   </tr>
</thead>
<tbody>
   <tr>
      <td>devtest</td>
      <td><a href="/#!/accounts/5">Account</a></td>
      <td>true</td>
      <td>0</td>
   </tr>
   <tr>
      <td>test@testing.com</td>
      <td><a href="/#!/accounts/40">Account</a></td>
      <td>true</td>
      <td>1</td>
   </tr>

search for: xpath=//body/div[2]/div/div[2]/div[2]/div/div/table/tbody/tr[1]/td[1]


Solution

  • If you need to verify that last table row contains certain text you can use the Page Should Contain Element keyword and provide an xpath locator which will find the last table row containing your text:

    Page Should Contain Element    xpath=//table[@class="fw-table"]//tr[last()]//td[contains(text(),'bottomline')]