80

I would like to match the following text sometext12345_text using the below regex.

I'm using this in one of my selenium tests.

String expr = "//*[contains(@id, 'sometext[0-9]+_text')]";
driver.findElement(By.xpath(expr));

It doesn't seem to work though. Can somebody help?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
ragesh
  • 975
  • 1
  • 9
  • 12

3 Answers3

100

XPath 1.0 doesn't handle regex natively, you could try something like

//*[starts-with(@id, 'sometext') and ends-with(@id, '_text')]

(as pointed out by paul t, //*[boolean(number(substring-before(substring-after(@id, "sometext"), "_text")))] could be used to perform the same check your original regex does, if you need to check for middle digits as well)

In XPath 2.0, try

//*[matches(@id, 'sometext\d+_text')]
Robin
  • 9,415
  • 3
  • 34
  • 45
  • 4
    This seems to work: `//*[boolean(number(substring-before(substring-after(@id, "sometext"), "_text")))]`, but it's highly specific to this use case – paul trmbrth Jan 28 '14 at 13:36
  • 2
    Yup, regex support would obviously be stronger/more flexible. Nice trick though! – Robin Jan 28 '14 at 13:55
  • 13
    seems like 'ends-with()' comes only with XPath2/XQuery but not with XPath1.0, proofs: http://www.w3.org/TR/xpath/#section-String-Functions , http://www.w3.org/TR/xpath-functions/#func-ends-with – bor Jun 04 '15 at 08:36
  • 3
    How do I get to know which version of xpath is bein used in my selenium test? – Sakshi Singla Jan 04 '17 at 06:55
  • @Robin - could you please help me with a related question ? https://stackoverflow.com/questions/44292671/how-to-search-for-exact-text-using-xpath-and-regex – MasterJoe Jun 01 '17 at 17:39
  • 4
    couldn't use ends-with in xpath 1.0, but starts-with works perfectly :) – wei Nov 15 '17 at 22:26
11

In Robins's answer ends-with is not supported in xpath 1.0 too.. Only starts-with is supported... So if your condition is not very specific..You can Use like this which worked for me

//*[starts-with(@id,'sometext') and contains(@name,'_text')]`\
Harish Kiran
  • 111
  • 1
  • 3
6

If you're using Selenium with Firefox you should be able to use EXSLT extensions, and regexp:test()

Does this work for you?

String expr = "//*[regexp:test(@id, 'sometext[0-9]+_text')]";
driver.findElement(By.xpath(expr));
paul trmbrth
  • 20,518
  • 4
  • 53
  • 66
  • I'm using webdriver(2.39.0) with firefox(26.0). I tried the above suggestion but it results in error. `InvalidSelectorError: Unable to locate an element with the xpath expression //*[regexp:test(@id, 'sometext[0-9]+_text')] because of the following error: [Exception... "An attempt was made to create or change an object in a way which is incorrect with regard to namespaces" code: "14" nsresult: "0x8053000e (NamespaceError)" location: ""]` Thanks for helping. – ragesh Jan 28 '14 at 13:15
  • I'm not familiar with Selenium, but if you find a way to register the namespace `"http://exslt.org/regular-expressions"` under the `"regexp"` prefix, it could work – paul trmbrth Jan 28 '14 at 13:19
  • Looks like the issue is still open for registering namespaces (https://code.google.com/p/selenium/issues/detail?id=1694), and it's independent of the EXSLT extensions. There are signs of people doing it here http://stackoverflow.com/a/19130149/2572383 ... Ah no, I think the parsing there is done outside – paul trmbrth Jan 28 '14 at 13:21
  • @paultrmbrth - - could you please help me with a related question ? https://stackoverflow.com/questions/44292671/how-to-search-for-exact-text-using-xpath-and-regex – MasterJoe Jun 01 '17 at 17:39