2

In Selenium 2.42.2 and Firefox 29, what is wrong with this XPath expression using regex:

//button[matches(text(),'\s*ABC\s*')]

It gives the following error message:

[Exception... "The expression is not a legal expression."  code: "12" nsresult: "0x805b0033 (SyntaxError)"  location: "<unknown>"]
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Could you please share the reasons to unaccept the answer? May be there is smth to improve? Thanks. – alecxe Oct 03 '15 at 02:51

1 Answers1

1

matches() is a part of xpath 2.0. In terms of xpath support selenium webdriver relies on the browser, which, in your case is Firefox, which, as far as I understand, doesn't support xpath 2.0.

There are plenty of functions in 1.0 that can help you to overcome the issue.

For example, contains():

//button[contains(., 'ABC')]

If the text is at the beginning or end of the string, you can apply starts-with() or ends-with():

//button[starts-with(., 'ABC')]
//button[ends-with(., 'ABC')]

See also this relevant thread:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • @Solver good point, thank you. As far as I understand, Firefox doesn't support 2.0. See also: http://stackoverflow.com/questions/16658799/what-browsers-support-xpath-2-0. – alecxe Sep 14 '14 at 15:14
  • 1
    @Solver also found [this issue](https://bugzilla.mozilla.org/show_bug.cgi?id=396966), that is still open. This is probably a reliable sign of xpath 2.0 being not supported.. – alecxe Sep 14 '14 at 15:20