user729076 user729076 - 1 month ago 12
jQuery Question

WebDriverException: Unexpected token ':'. Expected ')' to end a argument list when using jQuery :contains with Selenium

I have the following code:

String jquery = "$(h4:contains('im looking for this')).get(0)";
WebElement element = (WebElement)((JavascriptExecutor) wd).executeScript(jquery);


and I get this error:


WebDriverException: Unexpected token ':'. Expected ')' to end a argument list.


I don't know why my jQuery string is incorrect/

Answer

You're missing quotes around the whole selector:

String jquery = "$(\"h4:contains('im looking for this')\").get(0)";