javamavenseleniumgeckodriverfirefox-headless

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. Any Help?

Code and Stack Trace Below:

Java Code

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 

Solution

  • Resolved it.The problem was that, I was using a corrupted copy of geckodriver and it gave the following error above the 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'

    Details of the issue can be found here: https://github.com/SeleniumHQ/selenium/issues/6013#issuecomment-396665924