1

I'm testing a page on which jQuery is loaded, using WebDriver and ChromeDriver. I'd like to use some jQuery selectors, particularly the :contains() pseudo-selector for finding a node based on the text in that node. However, when I try to pass selector strings that use jQuery selectors like :contains() or :has() into WebDriver's By.CssSelector() method, WebDriver throws an invalid argument exception. I thought that ChromeDriver just passed the CSS selector through to the underlying browser. Can anyone explain exactly where this is going wrong?

1

I think Selenium uses its own implementation, based on Sizzle, as describe in this blog post and this one on WebdriverIO.

You can always use the JavaScript Executor to use jQuery to find elements:

script = "return $('"+selector+"').get(0);"
element = webdriver.execute_script(script);

Code example from this blog post. Update it for the programming language you are using.

Here is an example in Java, which also injects jQuery if it is not yet loaded by the website itself.

  • Thanks - those were some informative posts. So it sounds from the first link like they actually don't use Sizzle in Selenium 2, as a deliberate choice to improve performance and instead rely on the native browser CSS support. That does match what I've seen when sending paths to WebDriver for parsing. – severalservals Mar 23 '16 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.