Selenium:无法在 Linux 上找到浏览器二进制文件



我目前正在尝试使用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

壁虎驱动程序在我的$PATH中,执行它会得到以下结果:

ann@ann-System-Product-Name:~$ geckodriver
1609693404322   geckodriver INFO    Listening on 127.0.0.1:4444
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 path 而不是 firefox.exe path。您可以使用上面的代码

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

GeckoDriver/Firefox

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

/usr/bin/firefox

如果 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中找到相关的详细讨论:无法连接到二进制FirefoxBinary(C:\Program Files\Mozilla Firefox\firefox.exe)与GeckoDriver Firefox和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");

您可以在ChromeDriver的默认位置以及在Windows上安装Chrome中找到相关的详细讨论


注意:如果您使用的是壁虎驱动程序/火狐组合,则必须使用webdriver.gecko.driver而不是webdriver.firefox.driver

相关内容

最新更新