Java Firefox 浏览器无法使用 selenium webbrowser 代码打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38751525/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:36:30  来源:igfitidea点击:

Firefox browser is not opening with selenium webbrowser code

javaeclipseseleniumfirefox

提问by SJR

Need guidance and help in the below one.

在下面需要指导和帮助。

When the below code is executed, I am getting error. I am using the latest version of java, eclipse, firefox, and WebDrive jar file.

执行以下代码时,出现错误。我正在使用最新版本的 java、eclipse、firefox 和 WebDrive jar 文件。

package firsttest1;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class helloselenium {
    public static void main(String[] args) {
        WebDriver driver;
        driver =new FirefoxDriver();
        String url ="http://www.google.com";
        driver.get(url);
    }
}

error....

错误....

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releasesat com.google.common.base.Preconditions.checkState(Preconditions.java:199) at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109) at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:38) at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:91) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296) at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:245) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:220) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:215) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:211) at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:124) at firsttest1.helloselenium.main(helloselenium.java:12)

线程“main”中的异常 java.lang.IllegalStateException:驱动程序可执行文件的路径必须由 webdriver.gecko.driver 系统属性设置;有关更多信息,请参阅https://github.com/mozilla/geckodriver。最新版本可从https://github.com/mozilla/geckodriver/releases下载在 com.google.common.base.Preconditions.checkState(Preconditions.java:199) 在 org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109) 在 org.openqa.selenium.firefox.GeckoDriverService .access$100(GeckoDriverService.java:38) 在 org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:91) 在 org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService. java:296) 在 org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:245) 在 org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:220) 在 org.openqa.selenium.firefox。 FirefoxDriver.(FirefoxDriver.java:215) 在 org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:211) 在 org.openqa.selenium.firefox。FirefoxDriver.(FirefoxDriver.java:124) 在 firsttest1.helloselenium.main(helloselenium.java:12)

Version used: eclipse : neon version java : jdk1.8 fire fox: v48 WebDrive jar file : v 3.0.0 beta2

使用的版本:eclipse:neon 版本 java:jdk1.8 fire fox:v48 WebDrive jar 文件:v 3.0.0 beta2

guide me in resolving this issue.

指导我解决这个问题。

回答by Nicolas Henneaux

Firefox driver is based on marionettestarting with Selenium 3.0. Unlike, 2.x versions, it requires an external executable file. You should add it to your path. For more information, you should have a look at https://github.com/mozilla/geckodriver.

Firefox 驱动程序基于从 Selenium 3.0 开始的marionette。与 2.x 版本不同,它需要一个外部可执行文件。您应该将其添加到您的路径中。有关更多信息,您应该查看https://github.com/mozilla/geckodriver

回答by BlackDeath

Download the lastest version for geckoDriver herethen set a System property called "webdriver.gecko.driver" and put on it the path to your geckoDriver executable path System.setProperty("webdriver.gecko.driver", "<path to your gecko driver executable>");

此处下载 geckoDriver 的最新版本,然后设置一个名为“webdriver.gecko.driver”的系统属性并将其放在 geckoDriver 可执行路径的路径上 System.setProperty("webdriver.gecko.driver", "<path to your gecko driver executable>");

回答by Paras

You are using latest version of Selenium WebDriveri.e. Selenium 3.x, this version of webdriver doesn't support direct firefox launch. You have to set the SystemPropertyfor webdriver.gecko.driver.

您使用的是最新版本的Selenium WebDriverie Selenium 3.x,此版本的 webdriver 不支持直接启动 firefox。您必须设置SystemPropertyfor webdriver.gecko.driver

Replace the Code:-

替换代码:-

WebDriver driver;
driver =new FirefoxDriver();

With This code:-

使用此代码:-

WebDriver driver;
System.setProperty("webdriver.gecko.driver", "<Path to your WebDriver>");
driver =new FirefoxDriver();

You can get the information about latest changes here

您可以在此处获取有关最新更改的信息

You can download the latest Geckodriver from here

您可以Gecko这里下载最新的驱动程序