I want to use Wildcards in my attributes. For example, this is my regular XPath:
//input[@id='activation:j_idt84:voId:1']`
I want to replace the j_idt
number with a wildcard because the number is dynamic. I'm looking for something like this:
//input[@id='activation:*:voId:1']
I don't know how to solve that issue. Is my idea even possible?
XPATH allows the use of wildcards to write more robust path expressions where the use of specific path expressions is either impossible or undesirable. matches any element node. matches any attribute node. matches any type of node.
XPath wildcard replaces the literal name of a given node making the Xpath more robust. And these wildcard steps are preferable when the XML element names are unknown and promoted as a shorthand notation to consider a set of elements.
We generally use an asterisk (*) while writing XPaths. This is called a Wildcard character. //input[@*='demo'] -> Any “input” node who contains an attribute value as “demo” for any attribute.
Globbing. Globbing in Selenium supports only three special characters: *, ?, and [ ]. * − matches any number of characters.
Unfortunately there's no string wildcards in XPath. However you can use multiple contains()
and starts-with()
to filter things like this.
//input[starts-with(@id, 'activation:') and contains(@id, ':voId:1')]
Also, this answer could be useful too: selenium: Is it possible to use the regexp in selenium locators
You can use string wildcards using the matches
function which is available in XPath 2.0:
//input[matches(@id, 'activation:.*:voId:1')]
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