Python Selenium 3.0 - 无法识别默认位置安装的Firefox 47.0.1(geckodriver)。

4

以下是我的环境:

  1. 操作系统:Windows 10 - 64位(家庭版)
  2. 浏览器:Firefox 47.0.1(32位)
  3. Python:2.7.10.12(64位)
  4. selenium:3.0.1
  5. Geckodriver:geckodriver-v0.11.1-win64.zip

Firefox安装在C:\Program Files (x86)\Mozilla Firefox目录下。

geckodriver.exe文件位于C:\Python27\Scripts目录中。

以下是我的Python代码:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.python.org")

这会导致以下错误:

Traceback (most recent call last):
  File "examples1.py", line 5, in <module>
    driver = webdriver.Firefox()
  File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 152, in __init__
    keep_alive=True)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

我的问题是尽管Firefox已经安装在默认位置,但webdriver无法找到它并抛出了错误。

注意:当我明确指定Firefox二进制文件的位置如下所示时,它可以工作。

binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)
2个回答

0

这个错误的原因是Python无法直接找到FirefoxBinary函数。

我遇到了类似的问题。通过引用该函数来解决它:

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

后来发现解决方案已经在这里(间接地)可用。

我不确定问题出在哪里。重新启动机器后,一切都正常,即使没有使用Firefox二进制位置指定位置,也可以从默认位置启动浏览器。我确定这不是导入问题。 - Naveen Kumar R B
2
我不知道这是什么意思:“Python无法直接找到FirefoxBinary函数”...但这个答案是不正确的。 - Corey Goldberg

0
重新启动我的计算机解决了这个问题。(如果您将geckodriver.exe放在其中一个PATH位置中,则可能需要这样做。)
不确定是否是真正需要解决的问题,但其中一个变量可能是原因。

关于 geckodriver.exe 和 Firefox 版本支持的一些背景知识:

来自 geckodriver 的 Github 页面:

Firefox 47 is explicitly not supported

如果您想使用 Firefox 47.0.1 版本,请使用 Firefox driver 而不是 geckodriver

  1. 在 selenium 2.53 的情况下,您无需进行任何其他操作(因为 selenium 2.53 默认使用 Firefox driver,无需设置 geckodriver)。
  2. 在 Selenium 3.0 中,我们必须设置 geckodriver 路径(因为在 Selenium 3.0 中,geckodriverFirefox 的默认驱动程序),使用 System.setProperty 并将 marionette 设置为 false,这样就可以禁用 geckodriver 功能并使用默认的 Firefox 驱动程序。

示例代码:

System.setProperty("webdriver.gecko.driver", "path/to/geckodriver.exe");
DesiredCapabilities d = new DesiredCapabilities();
d.setCapability("marionette", false);  // to disable marionette, by default true
WebDriver driver = new FirefoxDriver(d);

参考资料:

  1. https://github.com/mozilla/geckodriver#supported-firefoxen
  2. https://github.com/mozilla/geckodriver/issues/224
  3. https://dev59.com/Dpzha4cB1Zd3GeqPHZUM#40658421

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