selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”

0
0

I am working on selenium python in firefox. I am trying to find element by css selector

element = "span:contains('Control panel')"
my_driver
.find_element_by_css_selector(element)

I am getting below error

 raise exception_class(message, screen, stacktrace)
selenium
.common.exceptions.InvalidSelectorException: Message: Given css selector expression "span:contains('Control panel')" is invalid: InvalidSelectorError: 'span:contains('Control panel')' is not a valid selector: "span:contains('Control panel')"

In selenium IDE I am successfully able to find element by this field but in Python it is not working

  • You must to post comments
0
0

Using css_selector to locate element by text is not supported in Selenium (although it will work in the developer tools console). The only possibility is xpath

element = "//span[contains(text(), 'Control panel')]"
my_driver
.find_element_by_xpath(element)

Edit: a comment by @FlorentB:

A css selector is not supported by the console either, but JQuery supports it. The $('...') from the console is a shorthand for document.querySelector which is generally overridden with JQuery when the page has it.

  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website