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

selenium

Solution

The answer was exactly where I put the [last()] and I had it in the wrong place

It goes here:

//div[@id='content']//table//tr[last()]//td[contains(text(),'service')][last()]/following-sibling::td[2]

Not here:

//div[@id='content']//table//tr[last()]//td[contains(text(),'service')]/following-sibling::td[2][last()]

Problem

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.

Original source