3

I'm trying to automate web tests with Selenium and Java in Ubuntu OS.

Earlier versions of Selenium used to work very well with older version of Firefox in Ubuntu.

Since I upgraded to

  • Selenium 4+ (currently latest version 4.8.3) with
  • FireFox 111.0.1 (64-bit) in
  • Ubuntu 20.04.6 LTS

When I try to run this code:

public static void main(String[] args) {
    try {
        FirefoxDriver driver = new FirefoxDriver();

        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

        driver.get("https://www.google.com");

        Thread.sleep(5000);
        System.out.println(driver.getTitle());

        driver.close();
    } catch (Exception E) {
        E.printStackTrace();
    }
}

It results in this error:

1680156938202   geckodriver INFO    Listening on 127.0.0.1:21408
1680156938676   mozrunner::runner   INFO    Running command: "/snap/firefox/current/firefox.launcher" "--marionette" "--remote-debugging-port" "14860" "--remote-allow-ho ... s" "http://127.0.0.1:14860/,http://localhost:14860/,http://[::1]:14860/" "-no-remote" "-profile" "/tmp/rust_mozprofile84ChAY"
org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: Failed to start browser /snap/firefox/current/firefox.launcher: no such file or directory 
Host info: host: 'Aspire-ES1-571', ip: '127.0.1.1'
Build info: version: '4.8.3', revision: 'b19b418e60'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.15.0-69-generic', java.version: '17.0.5'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Command: [null, newSession {capabilities=[Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:debuggerAddress: true, moz:firefoxOptions: {}}], desiredCapabilities=Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:debuggerAddress: true, moz:firefoxOptions: {}}}]
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:148)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:106)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:165)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:183)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:229)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:157)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:150)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:146)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:131)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:127)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:112)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:102)
    at Demo.demo.main(demo.java:17)

I tried to look up possible solution for this error on the internet. I did find a few similar issues and solutions for ChromeDriver, but there is nothing related to 'FirefoxDriver'.

Can someone please help me resolve this?

1
  • I was able to "get rid" of this error by installing Firefox with snap. Now I've got another one, /snap/firefox/current/firefox.launcher: line 60: /snap/intellij-idea-community/464/usr/lib/firefox/firefox: No such file or directory xd
    – Line
    Nov 9 at 15:58

1 Answer 1

3

I can understand how frustrating it can be when the browser fails to launch, especially when using the latest versions of libraries. The issue you are encountering is that the GeckoDriver is unable to locate the Firefox Binary file in Ubuntu.

While newer versions of libraries often have fixes for issues in older versions, it seems that this particular issue won't be fixed in the GeckoDriver for Selenium 4+ anytime soon.

However, there is a workaround that you can try. Since the issue is with locating the Firefox Binary, you can find the path to the Firefox Binary file on Ubuntu and set up FirefoxOptions to use that path. This will tell the FirefoxDriver where to look for the Binary file and allow the browser to launch successfully.

Here's the sample code,

public static void main(String[] args) {
    try {
        FirefoxOptions ops = new FirefoxOptions();
        ops.setBinary("/usr/lib/firefox/firefox");
        FirefoxDriver driver = new FirefoxDriver(ops);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

        driver.get("https://www.google.com");

        Thread.sleep(5000);
        System.out.println(driver.getTitle());

        driver.close();
    } catch (Exception E) {
        E.printStackTrace();
    }
}

This code sets the path to the Firefox Binary file in the FirefoxOptions object and uses that object to create a new FirefoxDriver instance. With this code, the FirefoxDriver should be able to locate the Firefox Binary file and launch the browser successfully.

1
  • 1
    Awesome this is a good workaround until a proper solution is added to the Selenium library. Mar 30 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.