1

I'm currently trying to use Selenium to automate some browsing tasks on Linux (I'm running Linux Mint 20). However, I've ran into a brick wall: I can't get it to locate any browser binaries. I've tried with Firefox and Chromium (thinking that if I could get the Chromium binary to load I'd live with that), but they both yield the same result, Selenium says it can't find the browser binary.

Here is my code:

package test1;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class app
{
    public static void main(String[] args)
    {
        System.setProperty("webdriver.firefox.driver", "/home/ann/bin/geckodriver");
        WebDriver driver = new FirefoxDriver();
    }
}

Here are the results upon running the code:

Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: LINUX
Build info: version: '4.0.0-alpha-7', revision: 'de8579b6d5'
System info: host: 'ann-System-Product-Name', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.0-58-generic', java.version: '11.0.8'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:95)
    at java.base/java.util.Optional.orElseGet(Optional.java:369)
    at org.openqa.selenium.firefox.FirefoxOptions.getBinary(FirefoxOptions.java:199)
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.withOptions(GeckoDriverService.java:180)
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:177)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:155)
    at test1.app.main(app.java:10)
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Firefox is installed and can be executed from the terminal:

ann@ann-System-Product-Name:~$ whereis firefox
firefox: /usr/bin/firefox /usr/lib/firefox /etc/firefox

Geckodriver is in my $PATH and executing it gives the following results:

ann@ann-System-Product-Name:~$ geckodriver
1609693404322   geckodriver INFO    Listening on 127.0.0.1:4444
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Falador245
  • 11
  • 2

2 Answers2

1
    System.setProperty("webdriver.gecko.driver", "C:\\Users\\prave\\Downloads\\geckodriver.exe");
    File pathBinary = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
    FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);   
    
    FirefoxOptions options = new FirefoxOptions();
    options.setBinary(firefoxBinary);
    FirefoxDriver firefox=new FirefoxDriver(options);

System.setProperty("webdriver.firefox.driver", "/home/ann/bin/geckodriver"); sets the gekodriver path not firefox.exe path. You can use above code

and also its not webdriver.firefox.driver but webdriver.gecko.driver

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • After adapting your instructions for my Linux system, I still receive Exception in thread "main" java.lang.IllegalArgumentException: Path to the firefox binary must exist: /usr/lib/firefox/firefox at org.openqa.selenium.internal.Require$FileChecker.isFile(Require.java:203) at org.openqa.selenium.firefox.Executable.(Executable.java:40) at org.openqa.selenium.firefox.FirefoxBinary.(FirefoxBinary.java:118) at test1.app.main(app.java:15) – Falador245 Jan 03 '21 at 20:17
  • Pass the location of firefox exe change the path , don't use my location . Please add the code you used – PDHide Jan 03 '21 at 20:51
0

GeckoDriver / Firefox

On platforms the default location of firefox binary is:

/usr/bin/firefox

If is installed at the default location you can simply:

System.setProperty("webdriver.gecko.driver", "/home/ann/bin/geckodriver");
WebDriver driver = new FirefoxDriver();
    

However, the default location may vary for some of the linux distros. In those cases and in case firefox is installed at a customized location you have to:

System.setProperty("webdriver.gecko.driver", "/home/ann/bin/geckodriver");
FirefoxOptions options = new FirefoxOptions();
options.setBinary("/path/to/firefox");
WebDriver driver =  new FirefoxDriver(options);
driver.get("https://stackoverflow.com");
    

You can find a relevant detailed discussion in WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files\Mozilla Firefox\firefox.exe) with GeckoDriver Firefox and Selenium Java


ChromeDriver / Chrome

Similarly on linux platforms the default location of chrome binary is:

/usr/bin/google-chrome

If is installed at the default location you can simply:

System.setProperty("webdriver.chrome.driver", "/home/ann/bin/chromedriver");
WebDriver driver = new ChromeDriver();
    

However, the default location may vary for some of the linux distros. In those cases and in case chrome is installed at a customized location you have to:

System.setProperty("webdriver.chrome.driver", "/home/ann/bin/chromedriver");
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/chrome");
WebDriver driver =  new ChromeDriver(options);
driver.get("https://stackoverflow.com");
    

You can find a relevant detailed discussion in What is default location of ChromeDriver and for installing Chrome on Windows


Note: In case you are using geckodriver/firefox combo, instead of webdriver.firefox.driver you have to use webdriver.gecko.driver

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • When pointing to /usr/bin/firefox, I get this error even though that path in a terminal starts firefox Exception in thread "main" java.lang.IllegalArgumentException: Path to the firefox binary must exist: /usr/bin/firefox at org.openqa.selenium.internal.Require$FileChecker.isFile(Require.java:203) at org.openqa.selenium.firefox.Executable.(Executable.java:40) at org.openqa.selenium.firefox.FirefoxBinary.(FirefoxBinary.java:118) at test1.app.main(app.java:15) – Falador245 Jan 03 '21 at 20:23
  • @Falador245 `/usr/bin/firefox` isn't the actual firefox path. In the terminal type locate firefox or locate mozilla-firefox to get the actual path of firefox – undetected Selenium Jan 03 '21 at 21:19
  • `locate firefox` gives a ton of directories though. There's `/usr/lib/firefox/firefox` which works in a terminal but not with Selenium. – Falador245 Jan 03 '21 at 23:34