My XPath is not working for all the scenario

Viewed 74

I need to do assertion with the total of table and to do this when I am writing XPath for total in image table its not working for all the scenario. I know the reason because the table is dynamic so sometimes its have only 1 Image and sometime its have 5 Image so I am having difficulty to wrote x-path which works for all the scenario.

For the attached image I have written : enter image description here WebElement totalElement = driver.findElement(By.xpath("//*[@id="image_table"]/tbody/tr[5]/td[4]")); its running fine but when the table size changes. it failed.

for reference the webpage design is as below,

***<table class="table table-bordered table-striped table-hover display" id="image_table">
  <tr>
      <td colspan="2" class="text-right">Total</td>
      <td class="text-center">1,650</td>
      <td class="text-center">19,936</td>
      <td class="text-center">21,586</td> (trying to write xpath for this total)
  </tr>***
2 Answers

Try css:

#image_table>tr>td:last-of-type

It always defines the last element.

Update based on comment. This css selector is more robust:

#image_table tr:last-of-type>td:last-of-type
//td[contains(string(),"Total")]/following-sibling::td[last()]

find td that contains the text total and find the last following td

Related