【问题标题】:java.net.ConnectException: Failed to connect to localhost/127.0.0.1:xxxx while invoking Firefox browser in Headless modejava.net.ConnectException:在无头模式下调用 Firefox 浏览器时无法连接到 localhost/127.0.0.1:xxxx
【发布时间】:2018-11-22 00:53:29
【问题描述】:

我收到“java.net.ConnectException:无法连接到本地主机” 每当我尝试初始化 FirefoxDriver 时出错。有什么帮助吗?

下面的代码和堆栈跟踪:

Java 代码

firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");

System.setProperty("webdriver.gecko.driver", (String) pluginConfig.get("geckodriver"));
firefoxOptions = new FirefoxOptions();

firefoxOptions.setBinary(firefoxBinary);
driver = new FirefoxDriver(firefoxOptions);

这行报错

driver = new FirefoxDriver(firefoxOptions);

堆栈跟踪:

java.net.ConnectException: Failed to connect to localhost/127.0.0.1:5054 Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z' System info: host: 'xxxxx-Lenovo-YOGA-3-Pro-1370', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-127-generic', java.version: '1.8.0_171' Driver info: driver.version: FirefoxDriver
org.openqa.selenium.WebDriverException
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'xxxxxx-Lenovo-YOGA-3-Pro-1370', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-127-generic', java.version: '1.8.0_171'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:92)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:207)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)
    at 

【问题讨论】:

  • 操作系统:Ubuntu(Linux) 16.04 Selenium 版本:'3.12.0'

标签: java maven selenium geckodriver firefox-headless


【解决方案1】:

解决了。问题是,我使用了损坏的 geckodriver 副本,它在堆栈跟踪上方给出了以下错误。

--- exec-maven-plugin:1.2.1:exec (default-cli) @ fetcher-firefox ---
path/to/geckodriver: 2: path/to/geckodriver: Syntax error: newline unexpected
Exception in thread "main" org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:15615

构建信息:版本:'3.12.0',修订:'7c6e0b3',时间:'2018-05-08T14:04:26.12Z' 系统信息:主机:'xxxxx-Lenovo-YOGA-3-Pro-1370',ip:'127.0.1.1',os.name:'Linux',os.arch:'amd64',os.version:'4.4。 0-127-generic', java.version: '1.8.0_171'

可以在此处找到问题的详细信息: https://github.com/SeleniumHQ/selenium/issues/6013#issuecomment-396665924

【讨论】:

    【解决方案2】:

    不确定 pluginConfig 文件的内容。通常通过 Selenium 3.12.0GeckoDriver v 0.20.1 以编程方式在 Headless Mode 中调用 Firefox BrowserFirefox Quantum v60.0.2Java 您需要通过 FirefoxOptions 的实例传递 argument --headless > 你可以使用以下解决方案:

    • 代码块:

      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.openqa.selenium.firefox.FirefoxOptions;
      
      public class A_Firefox_Headless
      {
          public static void main(String[] args) 
          {
              System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
              FirefoxOptions options = new FirefoxOptions();
              options.addArguments("--headless");
              WebDriver driver =  new FirefoxDriver(options);
              System.out.println("Mozilla Firefox Headless Browser Initialized");
              driver.get("http://www.google.com");
              System.out.println("Page Title is : "+driver.getTitle());
              driver.quit();
          }
      }
      
    • 控制台输出:

      1528817592552   geckodriver INFO    geckodriver 0.20.1
      1528817592563   geckodriver INFO    Listening on 127.0.0.1:23550
      1528817593461   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "--headless" "-profile" "C:\\Users\\ATECHM~1\\AppData\\Local\\Temp\\rust_mozprofile.M2jv24VJ0QMg"
      *** You are running in headless mode.
      1528817597139   Marionette  INFO    Listening on port 8949
      1528817597577   Marionette  WARN    TLS certificate errors will be ignored for this session
      Jun 12, 2018 9:03:17 PM org.openqa.selenium.remote.ProtocolHandshake createSession
      INFO: Detected dialect: W3C
      Mozilla Firefox Headless Browser Initialized
      Page Title is : Google
      

    【讨论】:

    • System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver"); firefoxOptions = new FirefoxOptions(); firefoxOptions.addArguments("--headless"); driver = new FirefoxDriver(firefoxOptions);错误保持不变
    • 到目前为止,与我们环境的区别在于操作系统。我使用的是 Ubuntu (Linux),你的例子是 Windows。另外,您使用的是什么版本的 Selenium?
    • 您正在使用 Java,您还没有声明 firefoxOptionsdriver 的全部内容。您需要将 firefoxOptions 声明为 FirefoxOptions 的实例,并将 driver 声明为 WebDriver 的实例
    • firefoxOptions 在代码 sn-p 之外被声明为 FirefoxOptions。但是,驱动程序在代码 sn-p 之外被声明为 FirefoxDriver 而不是 WebDriver。我进行了更改,但仍然收到相同的错误。 System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver"); FirefoxOptions firefoxOptions = new FirefoxOptions(); firefoxOptions.addArguments("--headless"); WebDriver driver = new FirefoxDriver(firefoxOptions);
    • 需要将"/path/to/geckodriver"替换为geckodriver的绝对路径
    猜你喜欢
    • 1970-01-01
    • 2016-08-13
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 2014-03-04
    相关资源
    最近更新 更多