Java 如何在 Selenium 中使用 Gecko 可执行文件

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

How to use the gecko executable with Selenium

javaseleniumfirefoxgeckodriver

提问by Robert Reynolds

I'm using Firefox 47.0 with Selenium 2.53. Recently they have been a bug between Selenium and Firefox which make code not working. One of the solution is to use the Marionnette driver.

我使用 Firefox 47.0 和 Selenium 2.53。最近,它们是 Selenium 和 Firefox 之间的一个错误,导致代码无法正常工作。解决方案之一是使用 Marionnette 驱动程序。

I followed the instruction of this siteto use this new driver with a RemotWebDriver but I keep having the error :

我按照本网站的说明将这个新驱动程序与 RemotWebDriver 一起使用,但我一直遇到错误:

WARN - Exception: Exception in thread "main" org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/jgraham/wires. The latest version can be downloaded from ....

警告 - 异常:线程“main”org.openqa.selenium.WebDriverException 中的异常:驱动程序可执行文件的路径必须由 webdriver.gecko.driver 系统属性设置;有关更多信息,请参阅https://github.com/jgraham/wires。最新版本可以从...下载。

The code i've tried so far is very simple :

到目前为止我尝试过的代码非常简单:

public class Test {
    static WebDriver driver;
    static Wait<WebDriver> wait;
    public static void main(String[] args) throws MalformedURLException {
        System.setProperty("webdriver.gecko.driver", "C:\Selenium\geckodriver.exe");
        DesiredCapabilities cap = DesiredCapabilities.firefox();
        cap.setCapability("marionette", true);
        cap.setBrowserName("firefox");
        driver = new RemoteWebDriver(new URL("http://192.168.117.135:5555/wd/hub"), cap);//true to enable the JS
        wait = new WebDriverWait(driver, 3000);
        final String url = "https://www.google.com/";

        JavascriptExecutor js = (JavascriptExecutor) driver;

        try {
            driver.navigate().to(url);
        } finally {
            driver.close();
        }
    }
}

I'm sure that the path to the geckodriver.exe is right and i don't see where i did the mistake.

我确定 geckodriver.exe 的路径是正确的,但我看不出我在哪里做错了。

EDIT 1: I tried the following code :

编辑 1:我尝试了以下代码:

public class Test {
    static WebDriver driver;
    static Wait<WebDriver> wait;
    public static void main(String[] args) throws MalformedURLException {
        System.setProperty("webdriver.gecko.driver", "C:\Selenium\geckodriver.exe");

        driver = new MarionetteDriver();
        wait = new WebDriverWait(driver, 3000);
        final String url = "https://www.google.com/";

        JavascriptExecutor js = (JavascriptExecutor) driver;

        try {
            driver.navigate().to(url);
        } finally {
            driver.close();
        }
    }
}

and it's working it seems that the problem come from the RemoteWebDriver and the gecko driver, any of you have news on it ?

并且它正在工作,问题似乎来自 RemoteWebDriver 和壁虎驱动程序,你们有没有关于它的消息?

回答by Gecki

You need to specify the system property with the path the .exe when starting the Selenium server node. See also the accepted anwser to Selenium grid with Chrome driver (WebDriverException: The path to the driver executable must be set by the webdriver.chrome.driver system property)

启动 Selenium 服务器节点时,您需要使用 .exe 路径指定系统属性。另请参阅带有 Chrome 驱动程序的 Selenium 网格的已接受 anwser (WebDriverException:驱动程序可执行文件的路径必须由 webdriver.chrome.driver 系统属性设置)

回答by Purendra Agrawal

I am also facing the same issue and got the resolution after a day :

我也面临同样的问题,一天后得到解决:

The exception is coming because System needs Geckodriver to run the Selenium test case. You can try this code under the main Method in Java

出现异常是因为 System 需要 Geckodriver 来运行 Selenium 测试用例。您可以在 Java 中的 main Method 下尝试此代码

    System.setProperty("webdriver.gecko.driver","path of/geckodriver.exe");
    DesiredCapabilities capabilities=DesiredCapabilities.firefox();
    capabilities.setCapability("marionette", true);
    WebDriver driver = new FirefoxDriver(capabilities);

For more information You can go to this https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriverlink.

有关更多信息,您可以转到此https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver链接。

Please let me know if the issue doesn't get resolved.

如果问题没有得到解决,请告诉我。

回答by Mukesh otwani

Recently Selenium has launched Selenium 3 and if you are trying to use Firefox latest version then you have to use GeckoDriver:

最近 Selenium 推出了 Selenium 3,如果你想使用 Firefox 最新版本,那么你必须使用 GeckoDriver:

System.setProperty("webdriver.gecko.driver","G:\Selenium\Firefox driver\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

You can check full documentation from here

您可以从此处查看完整文档

回答by Do Nhu Vy

I create a simple Java application by archetype maven-archetype-quickstar, then revise pom.xml:

我通过原型maven-archetype-quickstar创建了一个简单的 Java 应用程序,然后修改 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>bar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>bar</name>
    <description>bar</description>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.0-beta3</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.0.0-beta3</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>3.0.0-beta3</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.0.0-beta3</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>bar</finalName>
    </build>
</project>

and

package bar;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AppTest {

    /**
     * Web driver.
     */
    private static WebDriver driver = null;

    /**
     * Entry point.
     * 
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        // Download "geckodriver.exe" from https://github.com/mozilla/geckodriver/releases
        System.setProperty("webdriver.gecko.driver","F:\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://localhost:8080/foo/");
        String sTitle = driver.getTitle();
        System.out.println(sTitle);
    }

}

You also use on Mac OS X, Linux: https://github.com/mozilla/geckodriver/releases

您也可以在 Mac OS X、Linux 上使用:https: //github.com/mozilla/geckodriver/releases

and

// On Mac OS X.
System.setProperty("webdriver.gecko.driver", "/Users/donhuvy/Downloads/geckodriver");

回答by George Papatheodorou

The solutions above work fine for local testing and firing up browsers from the java code.If you fancy firing up your selenium grid later then this parameter is a must have in order to tell the remote nodewhere to find the geckodriver:

上述做工精细本地测试,并从Java发射了浏览器的解决方案的代码。如果看中发射了您的硒网格后则此参数是一个必须以告知远程节点在哪里可以找到geckodriver:

-Dwebdriver.gecko.driver="C:\geckodriver\geckodriver.exe"

The node cannot find the gecko driver when specified in the Automation Java code.

在自动化 Java 代码中指定时,节点找不到 gecko 驱动程序。

So the complete command for the node whould be (assuming node and hub for test purposes live on same machine) :

所以节点的完整命令应该是(假设节点和集线器用于测试目的在同一台机器上):

java -Dwebdriver.gecko.driver="C:\geckodriver\geckodriver.exe" -jar selenium-server-standalone-2.53.0.jar -role node -hub http://localhost:4444/grid/register

And you should expect to see in the node log :

您应该期望在节点日志中看到:

00:35:44.383 INFO - Launching a Selenium Grid node
Setting system property webdriver.gecko.driver to C:\geckodriver\geckodriver.exe

回答by Moradnejad

I try to make it simple. You have two options while using Selenium 3+:

我试着让它变得简单。使用 Selenium 3+ 时有两种选择:

  • Either upgrade your Firefox to 47.0.1 or higher and use the default geckodriver of Selenium3.

  • Or disable using of geckodriver by specifying marionetteto false and use the legacy Firefox driver. a simple command to run selenium is: java -Dwebdriver.firefox.marionette=false -jar selenium-server-standalone-3.0.1.jar. You can also disable using geckodriver from other commands that are mentioned in other answers.

  • 将您的 Firefox 升级到 47.0.1 或更高版本并使用 Selenium3 的默认 geckodriver。

  • 或者通过指定marionette为 false 并使用旧版 Firefox 驱动程序来禁用 geckodriver 。一个简单的命令来运行硒是:java -Dwebdriver.firefox.marionette=false -jar selenium-server-standalone-3.0.1.jar。您还可以从其他答案中提到的其他命令中禁用 geckodriver。

回答by Boni García

You can handle the Firefox driver automatically using WebDriverManager.

您可以使用WebDriverManager自动处理 Firefox 驱动程序。

This library downloads the proper binary (geckodriver) for your platform (Mac, Windows, Linux) and then exports the proper value of the required Java environment variable (webdriver.gecko.driver).

该库为您的平台(Mac、Windows、Linux)下载正确的二进制文件 ( geckodriver),然后导出所需 Java 环境变量 ( webdriver.gecko.driver)的正确值。

Take a look at a complete example as a JUnit test case:

看一个完整的例子作为 JUnit 测试用例:

public class FirefoxTest {

  private WebDriver driver;

  @BeforeClass
  public static void setupClass() {
    WebDriverManager.firefoxdriver().setup();
  }

  @Before
  public void setupTest() {
    driver = new FirefoxDriver();
  }

  @After
  public void teardown() {
    if (driver != null) {
      driver.quit();
    }
  }

  @Test
  public void test() {
    // Your test code here
  }
}

If you are using Maven you have to put at your pom.xml:

如果您使用的是 Maven,则必须放置在您的pom.xml

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>4.0.0</version>
</dependency>

WebDriverManager does magic for you:

WebDriverManager 为你做魔法:

  1. It checks for the latest version of the WebDriver binary
  2. It downloads the WebDriver binary if it's not present on your system
  3. It exports the required WebDriver Java environment variables needed by Selenium
  1. 它检查最新版本的 WebDriver 二进制文件
  2. 如果您的系统上不存在 WebDriver 二进制文件,它会下载它
  3. 它导出 Selenium 所需的 WebDriver Java 环境变量

So far, WebDriverManager supports Chrome, Opera, Internet Explorer, Microsoft Edge, PhantomJS, and Firefox.

到目前为止,WebDriverManager支持ChromeOperaInternet ExplorerMicrosoft EdgePhantomJS,和Firefox

回答by ShiyamTJ

This can be due to system cannot find firefox installed location on path.

这可能是由于系统无法在路径上找到 Firefox 安装位置。

Try following code, which should work.

尝试以下代码,它应该可以工作。

System.setProperty("webdriver.firefox.bin","C:\Program Files\Mozilla Firefox\firefox.exe"); 
System.setProperty("webdriver.gecko.driver","<location of geckodriver>\geckodriver.exe");

回答by Carlos Caldas

It is important to remember that the driver(file) must have execution permission(linux chmod +x geckodriver).

重要的是要记住驱动程序(文件)必须具有执行权限(linux chmod +x geckodriver)。

To sum up:

总结:

  1. Download gecko driver
  2. Add execution permission
  3. Add system property:

    System.setProperty("webdriver.gecko.driver", "FILE PATH");

  4. Instantiate and use the class

    WebDriver driver = new FirefoxDriver();

  5. Do whatever you want

  6. Close the driver

    driver.close;

  1. 下载 Gecko 驱动程序
  2. 添加执行权限
  3. 添加系统属性:

    System.setProperty("webdriver.gecko.driver", "FILE PATH");

  4. 实例化并使用类

    WebDriver driver = new FirefoxDriver();

  5. 做你想做的

  6. 关闭驱动程序

    driver.close;

回答by Anish Pillai

I'm using FirefoxOptions class to set the binary location with Firefox 52.0, GeckoDriver v0.15.0 and Selenium 3.3.1 as mentioned in this article - http://www.automationtestinghub.com/selenium-3-0-launch-firefox-with-geckodriver/

我正在使用 FirefoxOptions 类来设置二进制位置与本文中提到的 Firefox 52.0、GeckoDriver v0.15.0 和 Selenium 3.3.1 - http://www.automationtestinghub.com/selenium-3-0-launch-firefox-带壁虎驱动程序/

The java code that I used -

我使用的java代码 -

FirefoxOptions options = new FirefoxOptions();
options.setBinary("C:\Program Files (x86)\Mozilla Firefox\firefox.exe"); //location of FF exe

FirefoxDriver driver = new FirefoxDriver(options);
driver.get("http://www.google.com");