Skip to main content
Bumped by Community user
edited title
Link
Younes
  • 111
  • 1
  • 4

CSS Selector using ':contains' fails with `invalid element state: Failed to execute 'querySelector' on 'Document'`'Document: is not a valid selector'`

Source Link
Younes
  • 111
  • 1
  • 4

CSS Selector using ':contains' fails with `invalid element state: Failed to execute 'querySelector' on 'Document'`

I'm new to Selenium. I'm using it with Python. I'm trying to use a CSS Selector with :contains but failed so far.

  • On Chrome, the selector is rejected with 'a:contains("mail")' is not a valid selector
  • On Firefox, it selects nothing and get timed out

Please help to point out what I'm doing wrong.

The snippet for Chrome is as follows:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Chrome('/path/to/chromedriver')

browser.get('https://www.google.com/')

try:
    WebDriverWait(browser, 15).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, 'a:contains("mail")'))
    )
finally:
    browser.quit()

Immediately, the following error is printed on the console:

Traceback (most recent call last):
  File "/some/path/test_contains.py", line 13, in <module>
    EC.presence_of_element_located((By.CSS_SELECTOR, 'a:contains("mail")'))
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 71, in until
    value = method(self._driver)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/expected_conditions.py", line 63, in __call__
    return _find_element(driver, self.locator)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/expected_conditions.py", line 311, in _find_element
    raise e
selenium.common.exceptions.InvalidElementStateException: Message: invalid element state: Failed to execute 'querySelector' on 'Document': 'a:contains("mail")' is not a valid selector.
  (Session info: chrome=52.0.2743.116)
  (Driver info: chromedriver=2.9.248304,platform=Linux 4.4.0-36-generic x86_64)

The snippet for Firefox is as follows:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Firefox()

browser.get('https://www.google.com/')

try:
    WebDriverWait(browser, 15).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, 'a:contains("mail")'))
    )
finally:
    browser.quit()

It ends with timeout:

Traceback (most recent call last):
  File "/some/path/test_contains.py", line 13, in <module>
    EC.presence_of_element_located((By.CSS_SELECTOR, 'a:contains("mail")'))
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
    at FirefoxDriver.annotateInvalidSelectorError_ (file:///tmp/tmpb2DcYS/extensions/[email protected]/components/driver-component.js:10744)
    at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpb2DcYS/extensions/[email protected]/components/driver-component.js:10775)
    at FirefoxDriver.prototype.findElement (file:///tmp/tmpb2DcYS/extensions/[email protected]/components/driver-component.js:10779)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpb2DcYS/extensions/[email protected]/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpb2DcYS/extensions/[email protected]/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpb2DcYS/extensions/[email protected]/components/command-processor.js:12608)