解决使用selenium的这一句driver.findElements(By.cssSelector("XXX"))时,元素id为数字开头会出现报错的问题

   在使用selenium搭建自己的自检测试平台时,遇到一个问题,使用这一句定位某个id组件的时候,总是报错。报错现象如下所示:
org.openqa.selenium.InvalidElementStateException: invalid element state: Failed to execute 'querySelectorAll' on 'Document': '#paramValueTable div#60002_60002001_0 > select.textInput' is not a valid selector.
(Session info: chrome=38.0.2125.122)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 x86) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 27 milliseconds
Build info: version: '2.25.0', revision: '17482', time: '2012-07-18 21:09:54'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_79'
Driver info: driver.version: RemoteWebDriver
Session ID: 5a0458394ba8566a9fdd8a33e0b412f2
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:498)
at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:287)
at org.openqa.selenium.remote.RemoteWebDriver.findElementsByCssSelector(RemoteWebDriver.java:349)
at org.openqa.selenium.By$ByCssSelector.findElements(By.java:417)
at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:256)
at comba.BaseTest.checkAllDiv(BaseTest.java:1189)
at comba.BaseTest.test(BaseTest.java:343)
at comba.BaseTest.(BaseTest.java:173)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at Runner.test(Runner.java:344)
at Runner.run(Runner.java:252)
at Runner.runMain(Runner.java:203)
at Runner.main(Runner.java:176)

 解决使用selenium的这一句driver.findElements(By.cssSelector(   为了以防我的定位没有问题,我用xPath方法来定位确没有问题,如下代码所示(第三行注释的代码为我用xpath原路径定位的代码,第二行代码为报错的代码):
解决使用selenium的这一句driver.findElements(By.cssSelector(


 在百思不得其解中,在百度知道中问答,有答友提供线索,貌似用By.cssSelector来定位组件时,#后面的id不能是数字开头的,数字开头会报错,而我要定位查询的id就是以数字开头的,组成的路径是这样“#paramValueTable div#60002_60002001_0 > input.textInput[type = "text"]”,所以会报错。
 具体可以详阅如下网址:
  http://stackoverflow.com/questions/20306204/using-queryselector-with-ids-that-are-numbers
  那这样子我就知道根源所在了,我用了另外路径组成方法来来成功定位。
   
  解决方案:
  driver.findElements(By.cssSelector("#paramValueTable div#60002_60002001_0 > input.textInput[type = ‘text’ "))
  改成
  driver.findElements(By.cssSelector("#paramValueTable div[id = '60002_60002001_0']> input.textInput[type = ‘text’ ")).这样子就没有问题了。

你可能感兴趣的:(selenium,java,CSS)