Tom Tom - 2 months ago 15
Java Question

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

findElement(By.xpath("//span[contains(text(),'string')]"));


Expect result:

findElement(By.xpath("//span[contains(text(),**String**)]"));


which string is an expression

Answer

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 +"')]"))