22

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?

Cache Staheli
  • 3,510
  • 7
  • 32
  • 51
Ed H
  • 513
  • 2
  • 5
  • 7

2 Answers2

37

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

Community
  • 1
  • 1
complex857
  • 20,425
  • 6
  • 51
  • 54
4

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

//input[matches(@id, 'activation:.*:voId:1')]
Sicco
  • 6,167
  • 5
  • 45
  • 61
  • This way doesn't work in my code. The solution of complex857 works. But thanks a lot! – Ed H Sep 20 '12 at 05:58
  • 3
    Selenium [delegates xpath to the underlying browser](http://code.google.com/p/selenium/issues/detail?id=3009) so once browsers implement them it should start working. – complex857 Sep 20 '12 at 06:14