Can we using regular expression in xpath value?

戏子无情 提交于 2019-12-06 02:48:59

问题


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?


回答1:


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:

  • Is string matches() supported in Selenium Webdriver 2?

Apply a starts-with() check instead:

//*[starts-with(@id, "ngdialog")]



回答2:


Using XPath 2.0 :

:xpath,'//*[matches(@id, '^(ngdialog)[0-9]')]/div[2]/div[2]/table/tbody/tr/td[1]/input'



回答3:


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')


来源:https://stackoverflow.com/questions/27879750/can-we-using-regular-expression-in-xpath-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!