user3083447
user3083447

Reputation: 147

webdriver find element css selector cannot cope with complex search, An invalid or illegal string was specified

I am having difficulty with the Webdriver .findElement() method, here is the code in question:

driver.findElement(By.cssSelector("div[id='container1'] > div + div > div)")).click();

When I use the the same CSS selector using CTRL + ALT + S selenium firefox add-on, it works fine but not using Webdriver, why?

Here is the stack trace:

org.openqa.selenium.WebDriverException: An invalid or illegal string was specified Command duration or timeout: 400 milliseconds Build info: version: '2.38.0', revision: 'bd32d4e', time: '2013-12-05 16:15:38' System info: host: 'NBMAN1691', ip: '10.1.5.171', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_45' Session ID: ad315717-ecc6-4ed3-8d4e-1a152b64ebfb Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=25.0.1}]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)    at java.lang.reflect.Constructor.newInstance(Unknown Source)    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)   at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)     at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)     at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)     at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:396)    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:432)    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)     at kSweet.Login.Test(Login.java:54)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)  at java.lang.reflect.Method.invoke(Unknown Source)  at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)  at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)   at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)   at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)     at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)  at org.testng.TestRunner.privateRun(TestRunner.java:767)    at org.testng.TestRunner.run(TestRunner.java:617)   at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)     at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)     at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)  at org.testng.SuiteRunner.run(SuiteRunner.java:240)     at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)     at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)  at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)     at org.testng.TestNG.run(TestNG.java:1057)  at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)     at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: An invalid or illegal string was specified Build info: version: '2.38.0', revision: 'bd32d4e', time: '2013-12-05 16:15:38' System info: host: 'xxxxx', ip: '10.1.5.101', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_45' Driver info: driver.version: unknown

Upvotes: 0

Views: 1490

Answers (1)

Justin Ko
Justin Ko

Reputation: 46826

You have an extra closing bracket at the end of the css-selector (right before the closing quotation).

Try:

driver.findElement(By.cssSelector("div[id='container1'] > div + div > div")).click();

Upvotes: 1

Related Questions