小编典典

seleniumxpath,如何选择表中的最后一个匹配元素?

selenium

给定

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

当我不知道n是什么时,如何选择最后一个’service-n’行?我尝试添加,[last()]但是没有用。

我有:

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

但它选择第一行,而我要最后一行。
我无法使用,tr[3]因为实际上’service-n’行的数量是动态的,并且变化很大。


阅读 1783

收藏
2020-06-26

共1个答案

小编典典

答案正是我把[last()]放在哪里,我把它放在错误的地方

它在这里:

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

不在这里:

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