在 PATH 中找不到 firefox 二进制文件。确保已安装 Firefox

Posted

技术标签:

【中文标题】在 PATH 中找不到 firefox 二进制文件。确保已安装 Firefox【英文标题】:Cannot find firefox binary in PATH. Make sure firefox is installed 【发布时间】:2014-01-23 21:40:23 【问题描述】:

在 Selenium Grid 中,我正在尝试执行一个简单的程序,但我收到了 Cannot find firefox binary in PATH,尽管我在代码中添加了二进制路径。 我的代码和错误如下。请需要帮助。提前致谢。

代码

package Sample;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class sample1 
     WebDriver driver;
     String  BaseURL,NodeURL;
@BeforeTest
  public void beforeTest() throws MalformedURLException 
     BaseURL="www.google.com";
     NodeURL="http://192.168.10.162:5566/wd/hub";
     DesiredCapabilities capa =DesiredCapabilities.firefox();
     capa.setBrowserName("firefox");
     capa.setCapability("binary", "C:\\Users\\praveenraj.d\\AppData\\Local\\Mozilla Firefox\\firefox.exe");
     capa.setPlatform(Platform.ANY);
     driver=new RemoteWebDriver(new URL(NodeURL),capa);
  
   @Test
  public void f() throws InterruptedException 
      driver.get(BaseURL);
      System.out.println(driver.getTitle());
      Thread.sleep(50000);
  
  @AfterTest
  public void afterTest() 
      driver.quit();
  
 

错误

[TestNG] Running:
  C:\Users\praveenraj.d\AppData\Local\Temp\testng-eclipse-1233453386\testng-customsuite.xml

FAILED CONFIGURATION: @BeforeTest beforeTest
org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: WIN8
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15'
System info: host: 'IN-IF-WS1-0154', ip: '192.168.10.162', os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.7.0_07'
Driver info: driver.version: FirefoxDriver
Command duration or timeout: 97 milliseconds
Build info: version: '2.31.0', revision: '1bd294d', time: '2013-02-27 20:53:56'
System info: os.name: 'Windows 8', os.arch: 'x86', os.version: '6.2', java.version: '1.7.0_13'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:187)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:111)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:129)
    at Sample.sample1.beforeTest(sample1.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.TestRunner.beforeRun(TestRunner.java:641)
    at org.testng.TestRunner.run(TestRunner.java:609)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: WIN8
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15'
System info: host: 'IN-IF-WS1-0154', ip: '192.168.10.162', os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.7.0_07'
Driver info: driver.version: FirefoxDriver
Build info: version: '2.31.0', revision: '1bd294d', time: '2013-02-27 20:53:56'
System info: os.name: 'Windows 8', os.arch: 'x86', os.version: '6.2', java.version: '1.7.0_13'
Driver info: driver.version: FirefoxDriver
    at org.openqa.selenium.firefox.internal.Executable.<init>(Executable.java:72)
    at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:59)
    at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:55)
    at org.openqa.selenium.firefox.FirefoxDriver.getBinary(FirefoxDriver.java:182)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:107)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.server.DefaultDriverFactory.callConstructor(DefaultDriverFactory.java:62)
    at org.openqa.selenium.remote.server.DefaultDriverFactory.newInstance(DefaultDriverFactory.java:56)
    at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:216)
    at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call(DefaultSession.java:1)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:170)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

SKIPPED CONFIGURATION: @AfterTest afterTest
SKIPPED: f

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 1, Skips: 1
===============================================


===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================

[TestNG] Time taken by org.testng.reporters.SuitehtmlReporter@93b52b: 27 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@3ba002: 22 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@fa930d: 6 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@1f9c5c8: 6 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@197f158: 6 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 4 ms

【问题讨论】:

您的日志文件提到了去年和两个版本的事实让我相信您对在那里安装的版本和 jar 有一点问题。 Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA 的可能重复项 【参考方案1】:
File pathToBinary = new File("C:\\user\\Programme\\FirefoxPortable\\App\\Firefox\\firefox.exe");
FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
FirefoxProfile firefoxProfile = new FirefoxProfile();       
WebDriver driver = new FirefoxDriver(ffBinary,firefoxProfile);

【讨论】:

【参考方案2】:

另一种选择是配置服务器而不是测试客户端。

配置从节点服务,让它知道firefox在哪里。安装位置可能会因节点而异,甚至需要在一个节点上运行多个服务来支持访问不同版本的FF。

java -jar "selenium-server-standalone-2.2.0.jar"
 -Dwebdriver.firefox.bin="C:\FirefoxCollection\Mozilla Firefox 36.0\firefox.exe"

【讨论】:

我正在使用 Firefox Portable,此解决方案已修复 the issue。 在 Mac 上,只需使用 /Applications/Firefox.app 代替。 :) Cool 搜索了几个这个可行的解决方案。谢谢。但我最好在 .yml 文件中使用它。你知道怎么做吗?【参考方案3】:

确保 firefox 必须安装在默认位置,例如 ->(c:/Program Files (x86)/mozilla firefox OR c:/Program Files/mozilla firefox,注意:在安装 firefox 时不要更改路径,使其安装在默认路径中) 如果 Firefox 安装在其他地方,则 selenium 会显示这些错误。

如果您在系统 (Windows) 环境变量中设置了 firefox,则将其删除或使用新的 firefox 版本路径更新。

如果您想在任何其他地方使用 Firefox,请使用以下代码:-

由于 FirefoxProfile 被贬低,我们需要使用 FirefoxOptions 如下:

新代码:

File pathBinary = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);   
DesiredCapabilities desired = DesiredCapabilities.firefox();
FirefoxOptions options = new FirefoxOptions();
desired.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options.setBinary(firefoxBinary));

以上代码的完整工作代码如下:

System.setProperty("webdriver.gecko.driver","D:\\Workspace\\demoproject\\src\\lib\\geckodriver.exe");
File pathBinary = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);   
DesiredCapabilities desired = DesiredCapabilities.firefox();
FirefoxOptions options = new FirefoxOptions();
desired.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options.setBinary(firefoxBinary));
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.co.in/");

从以下 URL 下载 geckodriver for firefox:

https://github.com/mozilla/geckodriver/releases

适用于旧硒罐版本的旧代码

File pathBinary = new File("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);
FirefoxProfile firefoxProfile = new FirefoxProfile();       
WebDriver driver = new FirefoxDriver(firefoxBinary, firefoxProfile);

【讨论】:

这是有用的信息,它属于 Selenium 常见问题解答。 您遇到什么错误?你在 Windows 上工作吗?您的 Firefox 在系统中的位置是什么? 不推荐使用此构造函数,建议使用 FirefoxOptions(参见 MSC 的回答)。 Documentation Thansk Keku .. 我已经按照你的建议更新了新代码【参考方案4】:

在您启动 selenium 服务器后,您是否将 firefox 添加到您的路径中?如果是这种情况,硒仍将使用旧路径。解决方案是tear down & restart selenium 以便它使用更新的 Path 环境变量。

要检查您的路径中是否正确添加了 firefox,您只需启动命令行终端“cmd”并在此处键入“firefox”+ ENTER。如果 firefox 启动,则一切正常,重新启动 selenium 服务器应该可以解决问题。

【讨论】:

【参考方案5】:

我在使用 64 位 Windows 和 64 位 Firefox 浏览器的 C# 中遇到了同样的问题,这似乎不适用于 Selenium。如果您使用的是 64 位浏览器,请尝试 32 位浏览器。

令人惊讶的是,32 位 Firefox 浏览器在 64 位 Windows 上运行良好。

已编辑以阐明我的帖子的意图。

【讨论】:

它确实提供了问题的答案。如果 OP 使用的是 64 位 Firefox 浏览器,解决办法可能是切换到 32 位浏览器。【参考方案6】:

我刚刚遇到了这个问题,但没有更改 PATH。

我的电脑是Win7,64位系统, 如果你也在使用 64 位系统,不妨试试:

    卸载当前的 Firefox。 在“C:\Program Files (x86)\Mozilla Firefox\”路径下安装新的 Firefox。

它必须在“Program Files (x86)”而不是“Program Files”下

希望对你有帮助。

【讨论】:

我正用头撞墙。在我的机器和测试人员的机器上一切正常,但在虚拟机上它甚至没有尝试启动。检查安装路径。是的。 “程序文件”,而不是“程序文件 (x86)”。【参考方案7】:

如果您使用firefox v50及以上版本,则需要添加gecko driver

使用以下示例代码:

File pathToBinary = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
FirefoxProfile firefoxProfile = new FirefoxProfile();
System.setProperty("webdriver.gecko.driver","C:\\Users\\Downloads\\selenium-java-3.0.1\\geckodriver.exe");       
WebDriver driver = new FirefoxDriver(ffBinary,firefoxProfile);

【讨论】:

【参考方案8】:

我在这里没有看到这个问题的 C# 答案。诀窍是在 FirefoxOptions 实例上设置 BrowserExecutableLocation 属性,并将其传递给驱动构造函数:

        var opt = new FirefoxOptions
        
            BrowserExecutableLocation = @"c:\program files\mozilla firefox\firefox.exe"
        ;
        var driver = new FirefoxDriver(opt);

【讨论】:

【参考方案9】:

最简单的做法是让您的网格节点将 Firefox 二进制路径注册为节点配置的一部分。它使用与客户端相同的功能,但允许您按名称选择浏览器并让节点找到它。

还请注意,您想要的功能是“firefox_binary”而不是“binary”。您可以在此 wiki 页面查看 Firefox 功能的完整列表:

https://code.google.com/p/selenium/wiki/DesiredCapabilities

【讨论】:

这是正确的答案。 Node 怎么知道在远程服务器上哪里可以找到二进制文件? 我无法得到你的解释。请解释 你传递的能力是错误​​的。根据我链接到的 wiki 页面,您提供了“二进制”,但它应该是“firefox_binary”。作为次要建议,您的客户不应该设置页面。网格节点应在其配置文件中指定路径,以便客户端只需请求 Firefox 浏览器,网格将启动正确的二进制文件。【参考方案10】:

这个问题可能是因为firefox驱动和你的firefox浏览器版本不兼容。下载最新版本的firefox驱动,兼容最新的火狐浏览器版本。

【讨论】:

【参考方案11】:
java -jar selenium-server-standalone-2.53.1.jar -Dwebdriver.firefox.bin="C:\Program Files (x86)\Mozilla Firefox\firefox.exe"

将 selenium jar 文件放到桌面,进入 cmd 运行上述命令。

【讨论】:

【参考方案12】:

您应该更改环境变量并在此处添加 firefox.exe 的路径。同样可以以编程方式完成How can I set/update PATH variable from within java application on Windows?。我在 Win8 上遇到了同样的问题。

【讨论】:

【参考方案13】:

这是由于代码中使用了 RemoteWebDriver,它试图在节点机器中调用 firefox 浏览器,如果您在节点机器的“PATH”环境变量中指定了 firefox 二进制位置,它将在从 Hub 执行时打开 firefox 浏览器。

【讨论】:

【参考方案14】:

我在 Windows 10-64 位操作系统上也遇到了同样的问题。

当我在我的电脑上安装 firefox 时,它的安装位置是 “C:\Program Files\Mozilla Firefox\firefox.exe”而不是“C:\Program Files (x86)\Mozilla Firefox”,因为操作系统是 64 位的,

所以我只需将“Mozilla Firefox”文件夹复制并粘贴到“C:\Program Files (x86)”文件夹中并执行 selenium 脚本,它对我有用。

【讨论】:

【参考方案15】:
System.setProperty("webdriver.gecko.driver", "D:\\Katalon_Studio_Windows_64-5.10.1\\configuration\\resources\\drivers\\firefox_win64\\geckodriver.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette", true);
        WebDriver driver = new FirefoxDriver(capabilities);
        DriverFactory.changeWebDriver(driver)

【讨论】:

【参考方案16】:

有时解决方案只是简单地卸载应用程序并重新安装。有类似的问题。在我的情况下工作。由于某些奇怪的原因,Firefox 没有安装在默认的“程序”文件夹中。再次安装,问题得到解决。

【讨论】:

【参考方案17】:

您也可以使用 chromeWebDriver。 最新版本在download

代码:

  System.setProperty("webdriver.chrome.driver","yourplace\chromedriver.exe");
  WebDriver driver = new ChromeDriver();

【讨论】:

【参考方案18】:

使用旧版本的 Firefox。 60.0.1 60.0.2 60.1.0 60.2.0 60.2.1 60.2.2 60.3.0 60.4.0 60.5.0 60.5.1 60.5.2 60.6.0 60.6.1 60.6.2 60.6.3 60.7.0 60.7.1 60.7.2 60.8.0 60.9.0

我已使用 60.6.3 并已解决问题。

【讨论】:

以上是关于在 PATH 中找不到 firefox 二进制文件。确保已安装 Firefox的主要内容,如果未能解决你的问题,请参考以下文章

exec:“powershell”:在 Terraform 中运行 local-exec 时在 $PATH 中找不到可执行文件

Docker / NodeJS: "exec: \"-d\": $PATH 中找不到可执行文件"

OCI 运行时执行失败:执行失败:(...)$PATH 中找不到可执行文件“:未知

Docker 命令在 Windows 上的 Git Bash 中不起作用(执行:“com.docker.cli”:在 %PATH% 中找不到可执行文件)

Mac+Python环境使用phantomjs时报在PATH中找不到

使用 fastlane 在 crashlytics 包中找不到提交二进制文件