Something like .//div[@id='foo\d+]
to capture div tags with id='foo123'
.
I'm using .NET, if that matters.
The XPath regex plugin is used to match style which contains one or more values unique from attributes and elements in our xml documents. XPath regex is helped us using locate the part of an attribute which stays consistent for identifying the element of the web in a web page.
We can use regex in locators in Selenium webdriver. This can be achieved while we identify elements with the help of xpath or css locator. Let us have a look at the class of an element in its html code. The class attribute value is gsc-input.
A Regular Expression in Xpath helps with using part of a locator attribute that stays constant to identify a web element on a web page. Because some times the values within the HTML code for the attributes change. For instance attributes would change every time the web page you are working on is refreshed.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).
As other answers have noted, XPath 1.0 does not support regular expressions.
Nonetheless, you have the following options:
starts-with()
and translate()
functions) like this:.//div [starts-with(@id, 'foo') and 'foo' = translate(@id, '0123456789', '') and string-length(@id) > 3 ]
Use EXSLT.NET -- there is a way to use its functions directly in XPath expressions without having to use XSLT. The EXSLT extension functions that allow RegEx-es to be used are: regexp:match()
, regexp:replace()
and regexp:test()
Use XPath 2.0/XSLT 2.0 and its inbuilt support for regular expressions (the functions matches(), replace() and tokenize())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With