在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
答案 0 :(得分:31)
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);
答案 1 :(得分:20)
另一种选择是配置服务器而不是测试客户端。
配置从属节点服务,以便它知道firefox的位置。安装位置可以在节点之间变化,甚至需要在节点上运行多个服务以支持对不同版本的FF的访问。
java -jar "selenium-server-standalone-2.2.0.jar"
-Dwebdriver.firefox.bin="C:\FirefoxCollection\Mozilla Firefox 36.0\firefox.exe"
答案 2 :(得分:15)
确保firefox必须安装在默认位置,如 - &gt;(c:/ Program Files(x86)/ mozilla firefox或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/");
从以下网址下载firefox的geckodriver:
https://github.com/mozilla/geckodriver/releases
旧代码适用于旧的selenium jar版本
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);
答案 3 :(得分:5)
启动selenium服务器后,是否在路径中添加了firefox?如果是这种情况,selenium仍将使用旧路径。解决方案是拆除&amp; 重新启动selenium ,以便它将使用更新的路径环境变量。
要检查路径中是否正确添加了firefox,您只需启动一个命令行终端&#34; cmd&#34;并键入&#34; firefox&#34; +在那里输入。如果firefox启动,那么一切都没问题,重新启动selenium服务器应该解决问题。
答案 4 :(得分:5)
我在使用64位Windows和64位Firefox浏览器的C#中遇到了同样的问题,它似乎不适用于Selenium。如果您使用的是64位浏览器,请尝试使用32位浏览器。
令人惊讶的是,32位Firefox浏览器在64位Windows上运行就好了。
编辑澄清我的帖子的意图。
答案 5 :(得分:2)
我刚刚遇到这个问题而没有改变PATH。
我的电脑是Win7,64位系统, 如果您还使用64位系统,则可能需要尝试:
它必须位于“Program Files(x86)”下,而不是“Program Files”
希望它可以提供帮助。
答案 6 :(得分:2)
如果您使用gecko driver
及更高版本,则需要添加firefox v50
。
使用以下示例代码:
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);
答案 7 :(得分:2)
我在这里没有看到C#的问题。诀窍是在FirefoxOptions实例上设置BrowserExecutableLocation属性,并将其传递给驱动程序构造函数:
var opt = new FirefoxOptions
{
BrowserExecutableLocation = @"c:\program files\mozilla firefox\firefox.exe"
};
var driver = new FirefoxDriver(opt);
答案 8 :(得分:1)
最简单的方法是让网格节点将Firefox二进制路径注册为节点配置的一部分。它使用与客户端相同的功能,但允许您按名称选择浏览器并让节点找到它。
另请注意,您需要的功能是“firefox_binary”而不是“二进制”。您可以在此Wiki页面上查看Firefox功能的完整列表:
答案 9 :(得分:1)
此问题可能是因为firefox驱动程序与firefox浏览器版本不兼容。下载与更新的Firefox浏览器版本兼容的最新版firefox驱动程序。
答案 10 :(得分:1)
java -jar selenium-server-standalone-2.53.1.jar -Dwebdriver.firefox.bin="C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
将selenium jar文件放在桌面上,转到cmd并运行上面的命令。
答案 11 :(得分:0)
您应该更改环境变量并添加firefox.exe的路径。同样可以通过编程How can I set/update PATH variable from within java application on Windows?完成。我在Win8上遇到了同样的问题。
答案 12 :(得分:0)
这是由于RemoteWebDriver在代码中使用,它试图在节点机器中调用firefox浏览器,如果你在&#34; PATH&#34;中指定了firefox二进制位置。节点机器的环境变量,它将从Hub执行它时打开firefix浏览器。
答案 13 :(得分:0)
我在Windows 10-64位操作系统上也面临同样的问题。
当我在我的电脑上安装firefox时,它的安装位置是 “C:\ Program Files \ Mozilla Firefox \ firefox.exe”而不是“C:\ Program Files(x86)\ Mozilla Firefox”,因为操作系统是64位,
所以我只是复制&amp;将“Mozilla Firefox”文件夹粘贴到“C:\ Program Files(x86)”文件夹中并执行selenium脚本,它对我有用。
答案 14 :(得分:0)
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)
答案 15 :(得分:0)
有时解决方案是简单地卸载应用程序并立即安装。有类似的问题。在我的情况下工作。由于某种奇怪的原因,Firefox 没有安装在默认的“程序”文件夹中。重新安装,问题得到解决。