Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

CSS selector :contains doesn't work with Selenium 2.0a7 #987

Closed
lukeis opened this issue Mar 2, 2016 · 9 comments
Closed

CSS selector :contains doesn't work with Selenium 2.0a7 #987

lukeis opened this issue Mar 2, 2016 · 9 comments

Comments

@lukeis
Copy link
Member

lukeis commented Mar 2, 2016

Originally reported on Google Code with ID 987

What steps will reproduce the problem?
1. just try to retrieve element by css locator and use :contains selector.

Java example:
WebDriver driver = new FirefoxDriver();
driver.get("http://localhost:8080/");
WebElement webElement = driver.findElement(By.cssSelector("a:contains('Text')"));


With FirefoxDriver it throws:
org.openqa.selenium.WebDriverException: An invalid or illegal string was specified

with HtmlUnitDriver it throws:
com.steadystate.css.sac.TestCSSParseException: Error in pseudo class or element. Invalid
token "Text". Was expecting one of: <S>, ")", <IDENT>.


What is the expected output? What do you see instead?
WebElement is found and no Exception threw.


What version of the product are you using? On what operating system?
Selenium 2.0a7


System info: Fedora13, 64b, kernel: '2.6.34.7-61.fc13.x86_64', java.version: '1.6.0_21'.

Reported by rbalent on 2010-11-14 02:07:28

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

AFAICT, the :contains pseudo-class isn't in the CSS spec and is not supported by either
Firefox or Chrome (even outside WebDriver).

Reported by jari.bakken on 2010-12-02 01:43:37

  • Status changed: WontFix

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

FWIW, contains does work in Selenium (I've never used webdriver). See "Matching by inner
text" in 
http://saucelabs.com/blog/index.php/2010/01/selenium-totw-css-selectors-in-selenium-demystified/

Reported by dara.lillis on 2011-04-13 21:02:07

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Issue 1547 has been merged into this issue.

Reported by dawagner on 2011-04-22 23:40:18

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

@dara.lil...

Hi you must be using the selenium 1.x....

Here we are discussing about Selenium 2.x....

I am also facing the same issue....

Did any one find any solution to this problem.....



Reported by manishmiet2006 on 2011-05-11 06:22:17

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

This has been a pressing problem for me because the organization that I work for decided
to scrap xpath locators for css selectors because of the saucelabs link in comment
2 and because of a presentation that was given at the SeConf about how much faster
tests run with CSS.  What we were not told was that WebDriver was not going to support
the Sizzle style CSS selectors that Se1 uses.  To make things worse the :contains will
work for browsers that don't natively support CSS selectors (IE7, IE8), which causes
inconsistencies between browsers and selectors.  I am of the position that an automation
tool should be able to support the same selectors across the board so that the same
tests can be run across multiple browsers.  

I would be more than willing to help create a By.sizzle class, since most of the existing
code for css selectors can be re-factored to add the support for sizzle in addition
to css.

Reported by 3M8455 on 2011-07-20 20:09:26

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Hi , I am facing same problem. 
 WebElement element = driver.findElement(By.cssSelector("css=.gwt-Anchor:eq(7)"));
Getting exception :

org.openqa.selenium.WebDriverException: An invalid or illegal string was specified
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version:
'1.6.0_25'
Driver info: driver.version: RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

Reported by s.rao1999 on 2011-08-09 19:10:48

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

You will need to use a valid cssSelector.  The eq pseudo-class is specific to the Sizzle
selector engine that Selenium 1.0 relied on.  Check the selenium-users google group
as there are several solutions available like the patch attached to issue 1687

Reported by 3M8455 on 2011-08-09 19:34:58

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

I'm using selenium 2.45.0 (Python) and I can confirm the issue. The ":contains('...')"
pseudo-class, as described in https://saucelabs.com/resources/selenium/css-selectors,
isn't recognized.

The error message and Stacktrace is:

     InvalidSelectorException: Message: The given selector h4:contains('Personen')
is either invalid or does not result in a WebElement. The following error occurred:
     InvalidSelectorError: An invalid or illegal selector was specified
     Stacktrace:
          at FirefoxDriver.annotateInvalidSelectorError_ (file:///var/folders/8v/qhcr59vs7cb5pbnzwsb4j_gh0000gp/T/tmp2Eer6F/extensions/fxdriver@googlecode.com/components/driver-component.js:10245)
          at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/8v/qhcr59vs7cb5pbnzwsb4j_gh0000gp/T/tmp2Eer6F/extensions/fxdriver@googlecode.com/components/driver-component.js:10276)
          at FirefoxDriver.prototype.findChildElement (file:///var/folders/8v/qhcr59vs7cb5pbnzwsb4j_gh0000gp/T/tmp2Eer6F/extensions/fxdriver@googlecode.com/components/driver-component.js:10283)
          at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/8v/qhcr59vs7cb5pbnzwsb4j_gh0000gp/T/tmp2Eer6F/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
          at DelayedCommand.prototype.executeInternal_ (file:///var/folders/8v/qhcr59vs7cb5pbnzwsb4j_gh0000gp/T/tmp2Eer6F/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
          at DelayedCommand.prototype.execute/< (file:///var/folders/8v/qhcr59vs7cb5pbnzwsb4j_gh0000gp/T/tmp2Eer6F/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)

My environment: Mac OS X 10.9.5, Python 2.7.9

Reported by peter.bittner on 2015-03-06 16:51:21

@lukeis
Copy link
Member Author

lukeis commented Mar 2, 2016

Reported by luke.semerau on 2015-09-17 18:11:05

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis lukeis closed this as completed Mar 2, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant