org.openqa.selenium.WebDriverException:通过Selenium v​​3.4.0和geckodriver-v0.20.1

时间:2018-05-23 14:31:20

标签: java selenium firefox selenium-webdriver geckodriver

我无法为以下脚本启动Firefox浏览器。

脚本#1:

System.setProperty("webdriver.gecko.driver", "D:\\selenium\\Geckodriver\\geckodriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);  
driver.close();

脚本#2:

System.setProperty("webdriver.gecko.driver", "D:\\selenium\\Geckodriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");

我的Firefox版本:45.0.1
Selenium Jar版本:selenium-server-standalone-3.4.0
Gecko驱动程序版本:geckodriver-v0.20.1-win32

我收到如下所述的错误消息:

Exception in thread "main" org.openqa.selenium.WebDriverException: connection refused
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'SYNCGDC4098', ip: '172.26.59.52', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_151'
Driver info: driver.version: FirefoxDriver
remote stacktrace: 
    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.W3CHandshakeResponse.lambda$new$0(W3CHandshakeResponse.java:57)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$getResponseFunction$2(W3CHandshakeResponse.java:104)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$22(ProtocolHandshake.java:365)
    at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
    at java.util.Spliterators$ArraySpliterator.tryAdvance(Unknown Source)
    at java.util.stream.ReferencePipeline.forEachWithCancel(Unknown Source)
    at java.util.stream.AbstractPipeline.copyIntoWithCancel(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.FindOps$FindOp.evaluateSequential(Unknown Source)
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.util.stream.ReferencePipeline.findFirst(Unknown Source)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:368)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:159)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:142)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:191)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
    at seleniumtips.BasicScript.openApp(BasicScript.java:10)
    at seleniumtips.BasicScript.main(BasicScript.java:18)

我做错了什么?

3 个答案:

答案 0 :(得分:0)

geckodriver releases可以看出,当前最新版本的geckodriver是v0.20.1。您应该使用当前最新版本的Firefox浏览器。

此外,您使用的是旧版本的selenium(来自here,目前最新的版本是3.12.0)。

您需要更新firefox浏览器和selenium独立服务器。

如果你想(被迫)使用最旧的(45.0.1),你应该使用相关的geckodriver。

答案 1 :(得分:0)

您可以按照@Davide Patti的说法分别升级 Selenium geckodriver 。但是,在 script1 这一行中,WebDriver driver = new FirefoxDriver(capabilities);将不支持Selenium 3.12.0,因为它{&#39;}已被弃用。

您可以试用的代码:

FirefoxOptions foptions =  new FirefoxOptions();  
foptions.setCapability("marionette", true);  
WebDriver driver = new FirefoxDriver(foptions);

答案 2 :(得分:0)

此错误消息......

Exception in thread "main" org.openqa.selenium.WebDriverException: connection refused
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'

...意味着由于驱动程序无法启动其内部服务器与 Java客户端进行通信而引发WebDriverException

为了简单起见,您正在使用 Selenium Java Client v3.4.0 Firefox v45.0.1 ,您需要使用兼容的 GeckoDriver 二进制的。

  

强烈建议使用Geckodriver 0.16

  • 因此,您必须将GeckoDriver二进制版本降级为 0.16.0

  • 现在您正在使用 Selenium Java Client v3.4.0 GeckoDriver v0.16.0 Firefox v45.0.1 必须使用System.setProperty()行传递 webdriver.gecko.driver以及 Value 作为GeckoDriver二进制文件的绝对路径最后通过DesiredCapabilities类的实例将功能 marionatte 设置为 false ,然后使用merge()类中的MutableCapabilities方法将功能合并到FirefoxOptions实例的实例中以启动 Firefox浏览器,如下所示:

    System.setProperty("webdriver.gecko.driver", "C:/path/to/geckodriver.exe");
    DesiredCapabilities dc = new DesiredCapabilities();
    dc.setCapability("marionatte", false);
    FirefoxOptions opt = new FirefoxOptions();
    opt.merge(dc);
    FirefoxDriver driver =  new FirefoxDriver(opt);
    driver.get("https://stackoverflow.com");
    System.out.println("Application opened");
    System.out.println("Page Title is : "+driver.getTitle());
    driver.quit();
    

您可以在以下网址找到类似的讨论: