Tom Tom - 1 year ago 127
Java Question

How to use an expression in a findElement(By.xpath("//span[contains(text(),'string')]"))


Expect result:


which string is an expression

Answer Source

Actually Selenium-WebDriver uses the Wicked Good XPath library that only supports XPath 1.0.

So, regex expression is illegal here and you can use only features and functions of XPath 1.0 using Selenium-WebDriver.

Can I use a parameter value here if it does not support the regex expression

Yes, you can use parameter value but this parameter value would only treat as string instead of regex expression as below :-

String param = "something";

findElement(By.xpath("//span[contains(text(),'" + param +"')]"))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download