如何在Linux中的selenium中设置firefox的Firefox二进制路径?

时间:2017-02-07 15:43:44

标签: linux selenium firefox

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: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'skalia', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.19.0-25-generic', java.version: '1.8.0_111'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.firefox.internal.Executable.<init>(Executable.java:75)
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:60)
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:56)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:127)
at pack.SeleTest.main(SeleTest.java:10)

这是我在运行selenium脚本时遇到的错误,它在Window PC上工作正常。我设置了所有Build路径。添加所有硒罐。 帮我解决一下。

2 个答案:

答案 0 :(得分:2)

IN JAVA语言:

如果你在默认位置安装firefox,你只需写:

WebDriver driver = new FirefoxDriver();

对于其他位置,您可以编码如下:

File browserAppPath = null;
if (Platform.getCurrent().is(Platform.WINDOWS)) {
    browserAppPath = new File("C:\\Program Files\\Mozilla   Firefox\\firefox.exe");
    if (!browserAppPath.exists()) {
       browserAppPath = new File("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
   }
} else {
   // Ubuntu
   browserAppPath = new File("/usr/bin/firefox/firefox-bin");
}
WebDriver driver = new FirefoxDriver( new FirefoxBinary(browserAppPath), new FirefoxProfile());

答案 1 :(得分:0)

将您的Firefox二进制文件添加到PATH变量使用FirefoxDriver的构造函数以FirefoxBinary编程方式设置它,如下所示:

FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile);

要在Linux上找到firefox二进制文件的路径,请在shell中运行此命令:

which firefox

如果这没有显示路径或显示错误消息,那么Linux系统上没有安装Firefox(如果它安装在Windows PC上,那么这就是它在那里工作的原因)。