Java 在 PATH 中找不到 firefox 二进制文件。确保安装了火狐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20950748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot find firefox binary in PATH. Make sure firefox is installed
提问by user2307574
In Selenium Grid I am trying to execute a simple program and I'm getting Cannot find firefox binary in PATH
though I have added the binary path in my code.
My code and the error are given below. Kindly need help. Thanks in advance.
在 Selenium Grid 中,我试图执行一个简单的程序,Cannot find firefox binary in PATH
尽管我在代码中添加了二进制路径,但我得到了。
我的代码和错误如下。需要帮助。提前致谢。
Code
代码
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();
}
}
Error
错误
[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.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
回答by olyv
You should change environment variable and add there path to firefox.exe. The same could be done programmatically How can I set/update PATH variable from within java application on Windows?. I had the same problem on Win8.
您应该更改环境变量并将路径添加到 firefox.exe。同样可以通过编程方式完成如何从 Windows 上的 Java 应用程序中设置/更新 PATH 变量?. 我在Win8上遇到了同样的问题。
回答by nirvdrum
The easiest thing to do is have your grid nodes register the Firefox binary path as part of the node config. It uses the same capabilities that the client has, but allows you to pick the browser by name and let the node find it.
最简单的方法是让您的网格节点将 Firefox 二进制路径注册为节点配置的一部分。它使用与客户端相同的功能,但允许您按名称选择浏览器并让节点找到它。
Please also note that the capability you want is "firefox_binary" and not "binary". You can see the full list of Firefox capabilities at this wiki page:
另请注意,您想要的功能是“firefox_binary”而不是“binary”。您可以在此 wiki 页面上查看 Firefox 功能的完整列表:
回答by Shubham Jain
Make sure that firefox must install on default place like ->(c:/Program Files (x86)/mozilla firefox OR c:/Program Files/mozilla firefox, note: at the time of firefox installation do not change the path so let it installing in default path) If firefox is installed on some other place then selenium show those error.
确保 firefox 必须安装在默认位置,如 ->(c:/Program Files (x86)/mozilla firefox OR c:/Program Files/mozilla firefox,注意:在安装 firefox 时不要更改路径,所以让它在默认路径中安装)如果 Firefox 安装在其他地方,则 selenium 会显示这些错误。
If you have set your firefox in Systems(Windows) environment variable then either remove it or update it with new firefox version path.
如果您已在 Systems(Windows) 环境变量中设置了 Firefox,则将其删除或使用新的 Firefox 版本路径更新它。
If you want to use Firefox in any other place then use below code:-
如果您想在任何其他地方使用 Firefox,请使用以下代码:-
As FirefoxProfile is depricated we need to use FirefoxOptions as below:
由于 FirefoxProfile 已弃用,我们需要使用 FirefoxOptions,如下所示:
New Code:
新代码:
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));
The full working code of above code is as below:
上述代码的完整工作代码如下:
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/");
Download geckodriver for firefox from below URL:
从以下 URL 下载用于 Firefox 的 geckodriver:
https://github.com/mozilla/geckodriver/releases
https://github.com/mozilla/geckodriver/releases
Old Code which will work for old selenium jars versions
适用于旧硒罐版本的旧代码
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);
回答by Achille
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);
回答by Greg Domjan
Another option is to configure the server rather than the test client.
另一种选择是配置服务器而不是测试客户端。
Configure the slave node service so that it knows where the firefox is. Install location can change from node to node, or even need multiple services running on a node to support access to different versions of FF.
配置slave节点服务,让它知道firefox在哪里。安装位置可能会因节点而异,甚至需要在一个节点上运行多个服务来支持访问不同版本的 FF。
java -jar "selenium-server-standalone-2.2.0.jar"
-Dwebdriver.firefox.bin="C:\FirefoxCollection\Mozilla Firefox 36.0\firefox.exe"
回答by donfuxx
Did you add firefox to your path after you have started the selenium server? If that is the case selenium will still use old path. The solution is to tear down& restart seleniumso that it will use the updated Pathenvironment variable.
启动 selenium 服务器后,您是否将 firefox 添加到您的路径中?如果是这种情况,硒仍将使用旧路径。解决方案是拆除并重新启动 selenium,以便它使用更新的 Path环境变量。
To check if firefox is added in your path correctly you can just launch a command line terminal "cmd" and type "firefox" + ENTER there. If firefox starts then everything is alright and restarting selenium server should fix the problem.
要检查 firefox 是否正确添加到您的路径中,您只需启动命令行终端“cmd”并在那里键入“firefox”+ ENTER。如果 firefox 启动,则一切正常,重新启动 selenium 服务器应该可以解决问题。
回答by Rakesh
This is due to RemoteWebDriver used in code, It tries to invoke firefox browser in node machine, If you have specified firefox binary location in "PATH" environment variable of node machine, It will open firefix browser on executing it from Hub.
这是由于代码中使用了 RemoteWebDriver,它试图在节点机器中调用 firefox 浏览器,如果你在节点机器的“PATH”环境变量中指定了 firefox 二进制位置,它会在从集线器执行它时打开火狐浏览器。
回答by masospaghetti
I had the same issue in C# using 64-bit Windows and the 64-bit Firefox browser which doesn't seem to work on Selenium. If you are using the 64-bit browser, try the 32-bit one.
我在使用 64 位 Windows 和 64 位 Firefox 浏览器的 C# 中遇到了同样的问题,这似乎不适用于 Selenium。如果您使用的是 64 位浏览器,请尝试使用 32 位浏览器。
Surprisingly, the 32-bit Firefox browser runs on 64-bit Windows just fine.
令人惊讶的是,32 位 Firefox 浏览器在 64 位 Windows 上运行得很好。
Edited to clarify the intent of my post.
编辑以澄清我的帖子的意图。
回答by Harry Chu
I've just had this issue without changing PATH.
我刚刚遇到了这个问题,而没有更改 PATH。
My PC is Win7, 64-bit system, If you are also using 64-bit system, you may want to try:
我的电脑是Win7,64位系统,如果你也是64位系统,你可以试试:
- uninstall your current Firefox.
- install new Firefox under "C:\Program Files (x86)\Mozilla Firefox\" path.
- 卸载您当前的 Firefox。
- 在“C:\Program Files (x86)\Mozilla Firefox\”路径下安装新的 Firefox。
It must be under "Program Files (x86)" NOT "Program Files"
它必须在“Program Files (x86)”下,而不是“Program Files”
Hope it can help.
希望它能有所帮助。
回答by AZHAR WADIKAR
You need to add gecko driver
if you are using firefox v50
and above.
gecko driver
如果您使用firefox v50
及以上,则需要添加。
Use the following sample code :
使用以下示例代码:
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);