如何使用Gecko可执行文件与Selenium

42

我正在使用Firefox 47.0和Selenium 2.53。最近,Selenium和Firefox之间出现了一个bug,导致代码无法正常运行。其中一个解决方案是使用Marionnette驱动程序。

我按照这个网站的说明使用了新的驱动程序来创建RemotWebDriver,但我一直遇到以下错误:

警告 - 异常: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 ....

我尝试过的代码非常简单:

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();
        }
    }
}

我确信geckodriver.exe路径是正确的,但我看不出我犯了什么错误。

编辑 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();
        }
    }
}

看起来它正在工作,似乎问题来自RemoteWebDriver和gecko驱动程序,你们有任何消息吗?

10个回答

34

最近Selenium发布了Selenium 3,如果你想使用Firefox的最新版本,则必须使用GeckoDriver:

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

您可以在这里查看完整的文档


5
这个解决方案明显与Windows有关(因为涉及路径)。是否有一种平台无关的方法来做到这一点,或者需要为每台运行Selenium 3代码的机器进行设置? - Brick
1
我们通常会有一个环境特定的配置文件(测试机器IP、各种端口、路径等),并据此使用它,但是,我对不得不指定可执行文件路径才能运行测试并不太满意。如果有更好的想法,我会感激不尽。 - Tomislav Nakic-Alfirevic
针对 Mac 用户,您可以从此处下载 WebDriver: https://github.com/mozilla/geckodriver/releases。同时设置属性如下: System.setProperty("webdriver.gecko.driver", "/Users/gecko/geckodriver"); - clockworks

13

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

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

请参考完整示例作为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
  }
}

如果您正在使用Maven,您需要在 pom.xml 中加入以下内容:

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

WebDriverManager为您自动完成以下工作:

  1. 检查WebDriver二进制文件的最新版本
  2. 如果系统中不存在WebDriver二进制文件,则下载WebDriver二进制文件
  3. 导出Selenium所需的必要WebDriver Java环境变量

到目前为止,WebDriverManager支持 Chrome, Opera, Internet Explorer, Microsoft Edge, PhantomJSFirefox


12

我也遇到了同样的问题,经过一天的解决:

这个异常是因为系统需要Geckodriver才能运行Selenium测试用例。 你可以在Java的主方法中尝试这段代码。

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

更多信息请访问https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver

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


谢谢...我在你这里的第四行得到了Caused by: java.net.ConnectException: Connection refused: connect,你有什么想法吗? - mike rodent
据我理解,当远程拒绝连接时,ConnectException 异常会出现。你在第一行是否正确提供了 geckodriver 的路径? - Purendra Agrawal
哈哈!谢谢...最终搞定了。感谢你花费一整天宝贵的时间在这上面...我想知道"gecko"、"marionette"和"capability"这些东西都是什么?为什么使用Selenium 3.0.0必须要跨过这个障碍呢? - mike rodent

9
以下解决方案适用于本地测试和从Java代码中启动浏览器。如果你想稍后启动Selenium Grid,那么这个参数是必须的,以便告诉远程节点去哪里找到geckodriver:
-Dwebdriver.gecko.driver="C:\geckodriver\geckodriver.exe"

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

因此,对于节点来说,完整的命令应该是(假设节点和集线器出于测试目的位于同一台机器上):

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

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

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

1
给定的语法在新版本的selenium-server(如3.2.0)中会抛出异常:Exception in thread "main" com.beust.jcommander.ParameterException: Unknown option: -Dwebdriver.gecko.driver=geckodriver.exe at com.beust.jcommander.JCommander.parseValues(JCommander.java:742) ...为了避免这种情况,您需要更改参数顺序(先声明属性,再是jar和程序参数)。例如: java -Dwebdriver.gecko.driver="./geckodriver.exe" -jar selenium-server-standalone-3.2.0.jar -role node -hub http://10.64.201.100:4444/grid/register/ - voji

3

我尝试简单地阐述。在使用Selenium 3+时,您有两个选项:

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

  • 或者通过将marionette设为false来禁用使用geckodriver,并使用传统的Firefox驱动程序。运行Selenium的一个简单命令是:java -Dwebdriver.firefox.marionette=false -jar selenium-server-standalone-3.0.1.jar。您也可以从其他答案中提供的其他命令中禁用使用geckodriver。


1
谢谢,这对我很有帮助。我在Debian上安装Codeception,并且已经安装了Iceweasel。 - dmgig

3

我使用原型 maven-archetype-quickstart 创建了一个简单的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>

并且

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);
    }

}

你也可以在Mac OS X和Linux上使用:https://github.com/mozilla/geckodriver/releases,并且...
// On Mac OS X.
System.setProperty("webdriver.gecko.driver", "/Users/donhuvy/Downloads/geckodriver");

我尝试了给定的设置属性行,但我无法在Mac机器上启动Firefox浏览器。 - ChayanC

2
这可能是因为系统找不到Firefox安装位置所在的路径。尝试以下代码,应该可以解决问题。
System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe"); 
System.setProperty("webdriver.gecko.driver","<location of geckodriver>\\geckodriver.exe");

2

1
重要的是要记住驱动程序(文件)必须具有执行权限(Linux chmod +x geckodriver)。
总之:
  1. 下载gecko driver
  2. 添加执行权限
  3. 添加系统属性:

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

  4. 实例化并使用类

    WebDriver driver = new FirefoxDriver();

  5. 做任何你想做的事情

  6. 关闭驱动程序

    driver.close;


-1

我正在使用FirefoxOptions类来设置Firefox 52.0、GeckoDriver v0.15.0和Selenium 3.3.1的二进制位置,正如在这篇文章中提到的 - http://www.automationtestinghub.com/selenium-3-0-launch-firefox-with-geckodriver/

我使用的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");

不要提及geckodriver.exe,应按照Mukesh Otwani的答案建议使用。 - Valya
我不推荐使用 3.3.1 版本的 Selenium 驱动器,因为它存在特定问题。为了避免这个问题,最好使用 3.4.0 版本。 - invzbl3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接