简体   繁体   中英

Selenium xpath count

I must be overlooking something very obvious. What's wrong with this XPath expression? I want to get a count of 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','')]

尝试-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM