运行selenium remotedriver时出现带有NativeConstructorAccessorImpl.newInstance0的SessionNotCreatedException错误
我运行以下但得到错误
public class base { public static WebDriver driver; public static void main(String[] args) throws MalformedURLException, InterruptedException { System.setProperty("webdriver.chrome.driver", "C:\\code\\lib\\browser drivers\\chromedriver.exe"); String URL = "http://www.google.com"; String Node = "http://localhost:4444/wd/hub"; DesiredCapabilities cap = DesiredCapabilities.chrome(); cap = DesiredCapabilities.chrome(); cap.setPlatform(org.openqa.selenium.Platform.WINDOWS); driver = new RemoteWebDriver(new URL(Node), cap); driver.navigate().to(URL); Thread.sleep(5000); driver.quit(); } }
显示的错误如下:
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unable to create new service: ChromeDriverService Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z' System info: host: 'RAJESHW10', ip: '169.254.3.253', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_91' Driver info: driver.version: unknown Command duration or timeout: 316 milliseconds at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
请有人帮忙
该错误确实给了我们一个关于出错的提示,如下所示:
-
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unable to create new service: ChromeDriverService
: 无法创建新服务:ChromeDriverService表示未启动新会话。 -
Driver info: driver.version: unknown
: driver.version:unknown表示根本没有调用chromedriver二进制文件。 -
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
: sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ,它源于以下任何一种:java.lang.RuntimeException: exception in constructor
:此exception的出现对于reflection是唯一的。 通常,编写忽略已检查exception的代码是不可能的,因为它根本不会编译。java.lang.reflect.InvocationTargetException
:如果抛出InvocationTargetException
则表示调用了该方法。 此exception并不表示reflection包或其用法存在问题。java.lang.IllegalArgumentException
:如果构造函数传递了错误类型的参数,则抛出IllegalArgumentException
。
结论
从上面提到的ponints很明显, Selenium-Java
客户端, JDK
, ChromeDriver
二进制文件和Chrome
的版本不兼容。
解
解决方案如下:
- 使用最新版本更新
JDK
( JDK 8u152 ) - 使用最新版本更新
Selenium-Java
客户端( Selenium-Java v3.8.1 ) - 使用最新版本更新
ChromeDriver
二进制文件( ChromeDriver v2.34 ) - 使用最新版本更新
ChromeDriver
二进制文件( Chrome v63.0 )
请执行以下操作并重试。
确保将路径C:\\code\\lib\\browser drivers\\chromedriver.exe
添加到运行节点的计算机上的%PATH%
变量中。 这将确保selenium超级jar可以找到chromedriver二元的位置。
System.setProperty(“webdriver.chrome.driver”,“C:\ code \ lib \ browser drivers \ chromedriver.exe”);
此机制应仅在您执行时使用
ChromeDriver driver = ChromeDriver()
由于您正在使用selenium网格,因此您在此处理多个JVM。
System.setProperty()
只会影响当前的JVM(在本例中是您的测试),但实际的浏览器会在另一个JVM(运行selenium节点的JVM)中分离出来。