简体   繁体   English

java.net.ConnectException:在无头模式下调用Firefox浏览器时,无法连接到localhost / 127.0.0.1:xxxx

[英]java.net.ConnectException: Failed to connect to localhost/127.0.0.1:xxxx while invoking Firefox browser in Headless mode

I am getting a "java.net.ConnectException: Failed to connect to localhost" error whenever I try to initialize the FirefoxDriver. 每当我尝试初始化FirefoxDriver时,我都会收到“ java.net.ConnectException:无法连接到本地主机”错误。 Any Help? 有帮助吗?

Code and Stack Trace Below: 下面的代码和堆栈跟踪:

Java Code 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);

This line throws the error 这行抛出错误

driver = new FirefoxDriver(firefoxOptions);

Stack Trace: 堆栈跟踪:

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 

Not sure about the contents of pluginConfig file. 不确定pluginConfig文件的内容。 In general to invoke Firefox Browser in Headless Mode programatically through Selenium 3.12.0 , GeckoDriver v 0.20.1 , Firefox Quantum v60.0.2 and Java you need to pass the argument --headless through an instance of FirefoxOptions and you can use the following solution: 通常,要通过Selenium 3.12.0GeckoDriver v 0.20.1Firefox Quantum v60.0.2Java以编程方式在无头模式下调用Firefox浏览器 ,您需要将argument --headless通过FirefoxOptions实例传递 ,您可以使用以下解决方案:

  • Code Block: 代码块:

     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(); } } 
  • Console Output: 控制台输出:

     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 

Resolved it.The problem was that, I was using a corrupted copy of geckodriver and it gave the following error above the stacktrace. 解决了问题是,我使用的是geckodriver损坏的副本,它在stacktrace上方给出了以下错误。

--- 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

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' 内部版本信息:版本:'3.12.0',版本:'7c6e0b3',时间:'2018-05-08T14:04:26.12Z'系统信息:主机:'xxxxx-Lenovo-YOGA-3-Pro-1370', ip:'127.0.1.1',操作系统名称:'Linux',os.arch:'amd64',os.version:'4.4.0-127-generic',java.version:'1.8.0_171'

Details of the issue can be found here: https://github.com/SeleniumHQ/selenium/issues/6013#issuecomment-396665924 问题的详细信息可以在这里找到: https : //github.com/SeleniumHQ/selenium/issues/6013#issuecomment-396665924

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.net.ConnectException:无法连接到 localhost/127.0.0.1(端口 80):连接失败:ECONNREFUSED(连接被拒绝) - java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused) java.net.ConnectException:无法连接到 localhost/127.0.0.1(端口 8080):连接失败:ECONNREFUSED ....(代号一个应用程序) - java.net.ConnectException: fail to connect to localhost/127.0.0.1(port 8080): connect failed:ECONNREFUSED….(Codename One App) 无法连接到服务器:localhost / 127.0.0.1:9000:尝试一次失败。 java.net.ConnectException:连接被拒绝 - Failed to connect to server: localhost/127.0.0.1:9000: try once and fail. java.net.ConnectException: Connection refused 错误“org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/” with GeckoDriver 和 Firefox - error“ org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/” with GeckoDriver and Firefox org.openqa.selenium.WebDriverException: java.net.ConnectException: 无法使用 GeckoDriver Firefox 和 Selenium 连接到 localhost/0:0:0:0:0:0:0 - org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0 using GeckoDriver Firefox and Selenium IOException java.net.ConnectException:无法连接到/127.0.0.1(port 5000):connect failed:ECONNREFUSED(Connection Refused) - IOException java.net.ConnectException: failed to connect to /127.0.0.1(port 5000):connect failed:ECONNREFUSED(Connection Refused) 无法从 tomcat 应用程序连接到本地主机上的 postgrespql:原因:java.net.ConnectException:权限被拒绝(连接失败) - cannot connect to postgrespql on localhost from tomcat application: Caused by: java.net.ConnectException: Permission denied (connect failed) java.net.ConnectException:无法使用 ChromeDriver Chrome 通过 Spring Bot 中的 Selenium Java 连接到 localhost/0:0:0:0:0:0:0:1:1731 - java.net.ConnectException : Failed to connect to localhost/0:0:0:0:0:0:0:1:1731 using ChromeDriver Chrome through Selenium Java in Spring Bot org.openqa.selenium.WebDriverException: java.net.ConnectException: 无法使用 GeckoDriver 和 Selenium 连接到 localhost/0:0:0:0:0:0:0:1:1941 - org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost/0:0:0:0:0:0:0:1:1941 with GeckoDriver and Selenium org.openqa.selenium.WebDriverException:java.net.ConnectException:使用Gecko驱动程序无法连接到本地主机 - org.openqa.selenium.WebDriverException: java.net.ConnectException: Failed to connect to localhost with Gecko driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM