【问题标题】:Selenium using Java - The path to the driver executable must be set by the webdriver.gecko.driver system propertySelenium 使用 Java - 驱动程序可执行文件的路径必须由 webdriver.gecko.driver 系统属性设置
【发布时间】:2016-12-05 05:27:55
【问题描述】:

我正在尝试启动 Mozilla,但仍然收到此错误:

线程“main”中的异常 java.lang.IllegalStateException:驱动程序可执行文件的路径必须由 webdriver.gecko.driver 系统属性设置;有关详细信息,请参阅https://github.com/mozilla/geckodriver。最新版本可以从https://github.com/mozilla/geckodriver/releases下载

我正在使用Selenium 3.0.01 Beta 版和Mozilla 45。我也试过Mozilla 47。但还是一样。

【问题讨论】:

  • 我认为这不是一个重复的问题,它与selenium 3 一起出现,这是一个新问题,stacktrace 也不同。谢谢..:)
  • 您需要 geckodriver 在 Firefox 中启动 selenium 3 测试

标签: java selenium firefox


【解决方案1】:

我在 windows 10 中使用 selenium-java-3.141.59 并用这段代码解决了我的问题:

System.setProperty("webdriver.gecko.driver", "C:\\gecko\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin","C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();

【讨论】:

    【解决方案2】:

    在我的情况下,我必须在属性文件中设置路径,在许多小时内我找到了方法:

    application.properties 文件:

    webdriver.gecko.driver="/lib/geckodriver-v0.26.0-win64/geckodriver.exe"
    

    在java代码中:

    private static final Logger log = Logger.getLogger(Login.class.getName());
    private FirefoxDriver driver;
    private FirefoxProfile firefoxProfile;
    private final String BASE_URL = "https://www.myweb.com/";
    private static final String RESOURCE_NAME = "main/resources/application.properties"; // could also be a constant
    private Properties properties;
    
    public Login() {
        init();
    }
    
    private void init() {
        properties = new Properties();
        try(InputStream resourceStream = getClass().getClassLoader().getResourceAsStream(RESOURCE_NAME)) {
            properties.load(resourceStream);
        } catch (IOException e) {
            System.err.println("Could not open Config file");
            log.log(Level.SEVERE, "Could not open Config file", e);
        }
        // open incognito tab by default
        firefoxProfile = new FirefoxProfile();
        firefoxProfile.setPreference("browser.privatebrowsing.autostart", true);
        // geckodriver driver path to run
        String gekoDriverPath = properties.getProperty("webdriver.gecko.driver");
        log.log(Level.INFO, gekoDriverPath);
        System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + gekoDriverPath);
        log.log(Level.INFO, System.getProperty("webdriver.gecko.driver"));
        System.setProperty("webdriver.gecko.driver", System.getProperty("webdriver.gecko.driver").replace("\"", ""));
        if (driver == null) {
            driver = new FirefoxDriver();
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      selenium 中的每个 Driver 服务在创建驱动程序对象时都会调用类似的代码(以下是 firefox 特定的代码)

       @Override
       protected File findDefaultExecutable() {
            return findExecutable(
              "geckodriver", GECKO_DRIVER_EXE_PROPERTY,
              "https://github.com/mozilla/geckodriver",
              "https://github.com/mozilla/geckodriver/releases");
          }
      

      现在对于您要使用的驱动程序,您必须将系统属性设置为驱动程序可执行文件的路径值。

      for firefox GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver" 这可以在创建驱动程序对象之前设置,如下所示

      System.setProperty("webdriver.gecko.driver", "./libs/geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      

      【讨论】:

        【解决方案4】:

        Selenium WebDriver Java 代码:

        根据您的平台从https://github.com/mozilla/geckodriver/releases 下载 Gecko 驱动程序。将其提取到您选择的位置。编写如下代码:

        System.setProperty("webdriver.gecko.driver", "D:/geckodriver-v0.16.1-win64/geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.lynda.com/Selenium-tutorials/Mastering-Selenium-Testing-Tools/521207-2.html");
        

        【讨论】:

        • 位置可能因人而异。我在D盘上解压了
        【解决方案5】:
        1. 从 seleniumhq 网站下载 gecko 驱动程序(现在在 GitHub 上,你可以从Here 下载它)。
          1. 您将获得一个 zip(或 tar.gz),因此请解压缩它。
          2. 解压后你会得到 geckodriver.exe 文件(在 linux 中合适的可执行文件)。
          3. 在 C 中创建文件夹:命名为 SeleniumGecko(或适当)
          4. 将 geckodriver.exe 复制并粘贴到 SeleniumGecko
          5. 如下设置壁虎驱动的路径

        .

        System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        

        【讨论】:

        • 我错过了这个驱动程序的路径,现在它工作正常
        【解决方案6】:

        Selenium 客户端绑定将尝试从系统 PATH 中找到 geckodriver 可执行文件。您需要将包含可执行文件的目录添加到系统路径。

        • Unix 系统上,如果您使用 bash 兼容的 shell,您可以执行以下操作将其附加到系统的搜索路径:

          export PATH=$PATH:/path/to/geckodriver
          
        • Windows 上,您需要更新 Path 系统变量以将完整目录路径添加到可执行文件。原理和Unix上的一样。

        以下所有使用任何编程语言绑定启动最新 Firefox 的配置都适用于 Selenium2 以显式启用 Marionette。使用 Selenium 3.0 及更高版本,您无需执行任何操作即可使用 Marionette,因为它已默认启用。

        要在测试中使用 Marionette,您需要更新所需的功能以使用它。

        Java

        由于例外情况很明显,您需要从here 下载最新的geckodriver.exe,并将下载的geckodriver.exe 路径设置为您计算机中存在的系统属性,其中变量webdriver.gecko.driver 在启动木偶驱动程序并启动firefox 之前作为下面:-

        //if you didn't update the Path system variable to add the full directory path to the executable as above mentioned then doing this directly through code
        System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");
        
        //Now you can Initialize marionette driver to launch firefox
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette", true);
        WebDriver driver = new MarionetteDriver(capabilities); 
        

        对于Selenium3 使用:-

        WebDriver driver = new FirefoxDriver();
        

        If you're still in trouble follow this link as well which would help you to solving your problem

        .NET

        var driver = new FirefoxDriver(new FirefoxOptions());
        

        Python

        from selenium import webdriver
        from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
        
        caps = DesiredCapabilities.FIREFOX
        
        # Tell the Python bindings to use Marionette.
        # This will not be necessary in the future,
        # when Selenium will auto-detect what remote end
        # it is talking to.
        caps["marionette"] = True
        
        # Path to Firefox DevEdition or Nightly.
        # Firefox 47 (stable) is currently not supported,
        # and may give you a suboptimal experience.
        #
        # On Mac OS you must point to the binary executable
        # inside the application package, such as
        # /Applications/FirefoxNightly.app/Contents/MacOS/firefox-bin
        caps["binary"] = "/usr/bin/firefox"
        
        driver = webdriver.Firefox(capabilities=caps)
        

        红宝石

        # Selenium 3 uses Marionette by default when firefox is specified
        # Set Marionette in Selenium 2 by directly passing marionette: true
        # You might need to specify an alternate path for the desired version of Firefox
        
        Selenium::WebDriver::Firefox::Binary.path = "/path/to/firefox"
        driver = Selenium::WebDriver.for :firefox, marionette: true
        

        JavaScript (Node.js)

        const webdriver = require('selenium-webdriver');
        const Capabilities = require('selenium-webdriver/lib/capabilities').Capabilities;
        
        var capabilities = Capabilities.firefox();
        
        // Tell the Node.js bindings to use Marionette.
        // This will not be necessary in the future,
        // when Selenium will auto-detect what remote end
        // it is talking to.
        capabilities.set('marionette', true);
        
        var driver = new webdriver.Builder().withCapabilities(capabilities).build();
        

        使用RemoteWebDriver

        如果您想在任何语言中使用RemoteWebDriver,这将允许您在Selenium Grid 中使用Marionette

        Python

        caps = DesiredCapabilities.FIREFOX
        
        # Tell the Python bindings to use Marionette.
        # This will not be necessary in the future,
        # when Selenium will auto-detect what remote end
        # it is talking to.
        caps["marionette"] = True
        
        driver = webdriver.Firefox(capabilities=caps)
        

        红宝石

        # Selenium 3 uses Marionette by default when firefox is specified
        # Set Marionette in Selenium 2 by using the Capabilities class
        # You might need to specify an alternate path for the desired version of Firefox
        
        caps = Selenium::WebDriver::Remote::Capabilities.firefox marionette: true, firefox_binary: "/path/to/firefox"
        driver = Selenium::WebDriver.for :remote, desired_capabilities: caps
        

        Java

        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        
        // Tell the Java bindings to use Marionette.
        // This will not be necessary in the future,
        // when Selenium will auto-detect what remote end
        // it is talking to.
        capabilities.setCapability("marionette", true);
        
        WebDriver driver = new RemoteWebDriver(capabilities); 
        

        .NET

        DesiredCapabilities capabilities = DesiredCapabilities.Firefox();
        
        // Tell the .NET bindings to use Marionette.
        // This will not be necessary in the future,
        // when Selenium will auto-detect what remote end
        // it is talking to.
        capabilities.SetCapability("marionette", true);
        
        var driver = new RemoteWebDriver(capabilities); 
        

        注意:就像其他浏览器供应商提供给 Selenium 的其他驱动程序一样,Mozilla 现在已经发布了一个可以与浏览器一起运行的可执行文件。关注this了解更多详情。

        You can download latest geckodriver executable to support latest firefox from here

        【讨论】:

        • 非常感谢。这非常完美。但仅供参考,这个 geckodriver 到底是什么?我使用 selenium 已经有一段时间了,我之前没有遇到过这个问题。但是现在我刚刚更换了我的机器并得到了这个错误。
        • @Reema 就像其他浏览器供应商提供给 Selenium 的其他驱动程序一样,Mozilla 现在发布了一个可以与浏览器一起运行的可执行文件。看看更多细节..developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/…
        • 我决定寻找另一个很少做出像这样的重大更改的库。说真的,在我更新了 Firefox 或库后,通常它不再工作了。更糟糕的是,最新的geckodriver.exe for windows 仅适用于 64 位系统。是时候和 Selenium 说再见了。
        • 我真的很困惑。 “Selenium 客户端绑定将尝试定位 geckodriver” WebDriver 是否正在变成 Geckdriver?这是怎么回事?
        • @8protons 看看这里developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/… Mozilla 提供的 geckodriver,.. geckodriver 实现了 WebDriver 所以 selenium 客户端绑定尝试定位 geckodriver 以支持最新的 firefox 上的自动化
        猜你喜欢
        • 2017-02-04
        • 2017-09-11
        • 1970-01-01
        • 2017-12-21
        • 1970-01-01
        • 2017-03-16
        • 1970-01-01
        • 2017-09-07
        • 2020-03-14
        相关资源
        最近更新 更多