开发者

Selenium xpath count

I must be overlooking something very obvious. What's wrong with this XPath expression? I want to get a count o开发者_Python百科f table rows which match a regex for id?

selenium.getXpathCount("//tr[matches(@id,'data-row-\\d+')]");

I'm getting:

com.thoughtworks.selenium.SeleniumException: ERROR: Invalid xpath [2]: //tr[matches(@id,'data-row-\d+')]

Here's sample html:

<table>
  <tbody>
    <tr id="data-row-0"><td>foo</td></tr>
    <tr id="data-row-1"><td>bar</td></tr>
    <tr id="data-row-2"><td>baz</td></tr>
  </tbody>
</table>


Selenium 1.x does not support XPath 2.0. Hence you cannot use XPath 2.0 methods like matches(), replace() etc..

You might want to consider XPath 1.0 methods like contains(), starts-with() etc.

Also, to achieve your goal you can try

//tr[starts-with(@id,'data-row-') and translate(@id,'0123456789','')]


Try -

int tableRowCount = selenium.getXpathCount("//table[@id='whatever']/tbody/tr").intValue();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜