Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium xpath, how to select last matching element in a table?

Tags:

selenium

Given

<table>
  <tr>
    <td>service1</td>
  </tr>
  <tr>
    <td>service2</td>
  </tr>
  <tr>
    <td>service3</td>
  </tr>
  <tr>
    <td>blip</td>
  </tr>
</table>

How can I select the last 'service-n' row when I don't know what n will be? I have tried adding [last()] but it didn't work.

I have:

//table//tr//td[contains(text(),'service')]

but it selects the 1st row and I want the last one.
I can't use tr[3] because in reality the number of 'service-n' rows is dynamic and changes a lot.

like image 876
Michael Durrant Avatar asked Apr 03 '13 13:04

Michael Durrant


1 Answers

try with cssSelector, this way.

By.cssSelector("table tr:last-child td")
like image 106
e1che Avatar answered Sep 28 '22 08:09

e1che