【问题标题】:Selenium: Unable to Locate Browser Binaries on LinuxSelenium:无法在 Linux 上找到浏览器二进制文件
【发布时间】:2021-04-09 15:37:33
【问题描述】:

我目前正在尝试使用 Selenium 在 Linux 上自动执行一些浏览任务(我正在运行 Linux Mint 20)。但是,我遇到了障碍:我无法让它找到任何浏览器二进制文件。我尝试过使用 Firefox 和 Chromium(我想如果我可以加载 Chromium 二进制文件,我会接受),但它们都产生相同的结果,Selenium 说它找不到浏览器二进制文件。

这是我的代码:

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();
    }
}

以下是运行代码后的结果:

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 已安装并可从终端执行:

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

Geckodriver 在我的$PATH 中,执行它会得到以下结果:

ann@ann-System-Product-Name:~$ geckodriver
1609693404322   geckodriver INFO    Listening on 127.0.0.1:4444

【问题讨论】:

    标签: java selenium google-chrome selenium-webdriver firefox


    【解决方案1】:

    GeckoDriver / 火狐

    在 平台上,firefox 二进制文件的默认位置是:

    /usr/bin/firefox
    

    如果 安装在默认位置,您可以:

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

    但是,某些 Linux 发行版的默认位置可能会有所不同。在这些情况下,如果 firefox 安装在自定义位置,您必须:

    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");
        
    

    您可以在WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files\Mozilla Firefox\firefox.exe) with GeckoDriver Firefox and Selenium Java找到相关的详细讨论


    ChromeDriver / Chrome

    同样在 linux 平台上,chrome 二进制文件的默认位置是:

    /usr/bin/google-chrome
    

    如果 安装在默认位置,您可以:

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

    但是,某些 Linux 发行版的默认位置可能会有所不同。在这些情况下,如果 chrome 安装在自定义位置,您必须:

    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");
        
    

    您可以在What is default location of ChromeDriver and for installing Chrome on Windows找到相关的详细讨论


    注意:如果您使用 geckodriver/firefox 组合,则必须使用 webdriver.gecko.driver

    而不是 webdriver.firefox.driver

    【讨论】:

    • 当指向 /usr/bin/firefox 时,即使终端中的路径启动 firefox 异常在线程“main”java.lang.IllegalArgumentException:firefox 二进制文件的路径必须存在: /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) 在 test1.app.main(app.java:15)
    • @Falador245 /usr/bin/firefox 不是实际的 Firefox 路径。在终端输入locate firefox或者locate mozilla-firefox得到firefox的实际路径
    • locate firefox 提供了大量的目录。 /usr/lib/firefox/firefox 在终端中工作,但不适用于 Selenium。
    【解决方案2】:
        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");设置 gekodriver 路径不是 firefox.exe 路径。你可以使用上面的代码

    而且它不是 webdriver.firefox.driver 而是 webdriver.gecko.driver

    【讨论】:

    • 在为我的 Linux 系统调整您的指令后,我仍然在线程“main”java.lang.IllegalArgumentException 中收到异常:firefox 二进制文件的路径必须存在:/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) 在 test1.app.main(app.java:15)
    • 通过firefox exe的位置更改路径,不要使用我的位置。请添加您使用的代码
    猜你喜欢
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 2015-05-17
    • 2021-09-22
    • 1970-01-01
    • 2021-03-02
    • 2021-05-06
    相关资源
    最近更新 更多