1

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)
asked Sep 6, 2016 at 6:26

1 Answer 1

4

CSS: The CSS2 contains function is not in CSS3; however, Selenium supports the superset of CSS1, 2, and 3.

contains() is not part of the current CSS3 specification so it will not work on all browsers, only ones that implemented it before it was pulled.

have a look

Refer This For better understanding

answered Sep 6, 2016 at 7:03
1
  • Got it. I should then avoid :contains. For innertext (and text), it is not attribute. It cannot be used in an expression like the one you mention. That kind of expression is for attributes. Commented Sep 6, 2016 at 7:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.