user4039871
user4039871

Reputation:

Syntax error in selenium xpath expression using regex

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>"]

Upvotes: 2

Views: 1263

Answers (1)

alecxe
alecxe

Reputation: 473893

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:

Upvotes: 1

Related Questions