Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XPath wildcards in attributes in Selenium WebDriver

Tags:

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?

like image 209
Ed H Avatar asked Sep 19 '12 13:09

Ed H


People also ask

Can you use wildcard in XPath?

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.

What does the wildcard operator do in XPath?

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.

Which is the wildcard symbol in Selenium?

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.

Which wildcards are supported by Selenium?

Globbing. Globbing in Selenium supports only three special characters: *, ?, and [ ]. * − matches any number of characters.


2 Answers

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

like image 61
complex857 Avatar answered Oct 06 '22 07:10

complex857


You can use string wildcards using the matches function which is available in XPath 2.0:

//input[matches(@id, 'activation:.*:voId:1')]
like image 28
Sicco Avatar answered Oct 06 '22 09:10

Sicco