bsk
bsk

Reputation: 215

xpath+ regex: matches text

I'm trying to write an xpath such that only nodes with text with numbers alone will be returned. I wanted to use regex and was hoping this would work

td[matches(text(),'[\d.]')]

Can anyone please help me understand what am I doing wrong here

<tr>
        <td>1</td>
        <td>10</td>
        <td>a</td>
</tr>

Upvotes: 2

Views: 517

Answers (3)

Andersson
Andersson

Reputation: 52685

AFAIK so far Selenium support XPath 1.0 only, so matches() is not supported.

You can try below instead:

//td[number(.) >= 0 or number(.) < 0]

To match table cells with integers

Upvotes: 2

Dusan Gligoric
Dusan Gligoric

Reputation: 584

seams that you are missing quantification, [\d.] will match only 1 character, so 1 should be selected, 10 on the other site requires something like +, so try your regex like: td[matches(text(),'\d+')]

Also that . in regex will make it capture non-digit characters, do not add that one.

You can test all your regex queries on regex101.

Upvotes: 2

Andrei
Andrei

Reputation: 5647

Replace:

td[matches(text(),'[\d+]')]

with:

td[matches(text(),'\d+')]

Note: regex works only in xPath 2.0

Upvotes: 1

Related Questions