【问题标题】:Error SessionNotCreatedException with NativeConstructorAccessorImpl.newInstance0 while running selenium remotedriver运行 selenium 远程驱动程序时出现带有 NativeConstructorAccessorImpl.newInstance0 的错误 SessionNotCreatedException
【发布时间】:2018-06-12 14:02:52
【问题描述】:

我正在运行以下但收到错误

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)

有人可以帮忙吗

【问题讨论】:

标签: java selenium selenium-webdriver selenium-chromedriver selenium-grid


【解决方案1】:

请执行以下操作并重试。

确保将路径 C:\\code\\lib\\browser drivers\\chromedriver.exe 添加到运行节点的机器上的 %PATH% 变量中。这将确保 selenium uber jar 可以找到 chromedriver 二进制文件的位置。

System.setProperty("webdriver.chrome.driver", "C:\code\lib\browser drivers\chromedriver.exe");

这个机制应该只在你做的时候使用

ChromeDriver driver = ChromeDriver()

由于您使用的是 selenium 网格,因此您在这里处理的是多个 JVM。

System.setProperty() 只会影响当前的 JVM(在这种情况下是您的测试),但实际的浏览器会在不同的 JVM(运行 selenium 节点的那个)中分离出来。

【讨论】:

    【解决方案2】:

    该错误确实为我们提供了有关问题所在的提示,如下所示:

    • 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 :此异常的发生是反射独有的。通常,不可能编写忽略已检查异常的代码,因为它根本无法编译。

      java.lang.reflect.InvocationTargetException :如果抛出InvocationTargetException,则表示该方法已被调用。此异常并不表示反射包或其使用存在问题。

      java.lang.IllegalArgumentException:如果向构造函数传递了错误类型的参数,则会抛出 IllegalArgumentException。


    结论

    从上面提到的点很明显,Selenium-Java客户端的版本,JDK,ChromeDriver二进制和 Chrome 不兼容。


    解决方案

    解决方案如下:

    • 使用最新版本 (JDK 8u152) 更新您的 JDK
    • 使用最新版本更新您的 Selenium-Java 客户端 (Selenium-Java v3.8.1)
    • 使用最新版本更新您的 ChromeDriver 二进制文件 (ChromeDriver v2.34)
    • 使用最新版本 (Chrome v63.0) 更新您的 ChromeDriver 二进制文件

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      相关资源
      最近更新 更多