Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions in xpaths

I am using Java and Selenium. The HTML designers did not give really useful IDs. I need to access an element with the following:

<span id="gridcolumn-1040-textInnerEl" 

I can use

//span[contains(@id, 'gridcolumn') and (contains(@id, 'textInnerEl'))]

for the xpath but this can get complicated (this is a simpler example). is there any way to use a regexp in an xpath such as the following:

//span[match(@id, '^gridcolumn-[%d]+-testInnerEl$']

to avoid multiple and contains(@id,...)

like image 806
Tony Avatar asked Feb 05 '26 06:02

Tony


1 Answers

Try to use below XPath to match span by regex:

//span[matches(@id, '^gridcolumn-\d+-textInnerEl$')]
like image 142
Andersson Avatar answered Feb 08 '26 03:02

Andersson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!