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

2 Answers 2

4

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 for creating the driver with the path in options:

public static void main(String[] args) {
    try {
        FirefoxOptions options = new FirefoxOptions();
        options.setBinary("/usr/lib/firefox/firefox");
        FirefoxDriver driver = new FirefoxDriver(options);
        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.

3
  • on my CentOS the correct path (after installing Firefox from yum repository was /usr/bin/firefox. I found it by running whereis firefox
    – Line
    Dec 27, 2023 at 11:08
  • there are a couple of bugs about that, but closed because of no feedback, so I created a new one: github.com/SeleniumHQ/selenium/issues/13366
    – Line
    Dec 27, 2023 at 12:04
  • it seems fixed in the newest version of Selenium.
    – Line
    Dec 28, 2023 at 10:38
1

This is a bug in GeckoDriver (fixed in Selenium 4.16.1 or earlier).

To avoid changes in the code, upgrade or apply a workaround of creating a symbolic link:

sudo mkdir -p /snap/firefox/current
sudo ln -s /usr/bin/firefox /snap/firefox/current/
sudo mv /snap/firefox/current/firefox /snap/firefox/current/firefox.launcher

(-p is for creating all non-existing directories on the way, -s is for symbolic link and the last line is needed because the names of both executables differ)

Your Answer

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

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