7

Can we using regular expression in xpath value? I am using xpath value to identify an element on web for automation.

I have following :xpath value.

:xpath,'//*[@id="ngdialog4"]/div[2]/div[2]/table/tbody/tr/td[1]/input'

But, the last digit 4 ngdialog4 is not constant and it keeps on changing each time i open pop-up... can i use some regular expression to match any digit?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
RayM
  • 141
  • 1
  • 2
  • 11

3 Answers3

7

You could have theoretically used matches(), but it is a part of xpath 2.0, which webdriver doesn't support, see a detailed explanation here:

Apply a starts-with() check instead:

//*[starts-with(@id, "ngdialog")]
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • If the browser has an XPath engine, WebDriver will use it. If the browser's XPath engine doesn't support it, WebDriver won't. If the browser doesn't have an XPath engine (typically IE), then WebDriver places it's own library to use. So saying "It doesn't support XPath 2" is not correct at all. – Arran Jan 12 '15 at 10:03
0

Using XPath 2.0 :

:xpath,'//*[matches(@id, '^(ngdialog)[0-9]')]/div[2]/div[2]/table/tbody/tr/td[1]/input'
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
0

Since you are using Watir, you could also use a regular expression to find the ngdialog element. XPath could then be used for the other portion of the path (note the starting of the XPath with ./ is used to tell Watir to look within the ngdialog):

browser.element(:id => /ngdialog/).checkbox(:xpath => './div[2]/div[2]/table/tbody/tr/td[1]/input')
Justin Ko
  • 46,526
  • 5
  • 91
  • 101