4

以下是我的环境:

  1. 操作系统:Windows 10 - 64(家庭版)
  2. 浏览器:Firefox 47.0.1(32 位)
  3. Python:2.7.10.12(64 位)
  4. 硒:3.0.1
  5. 壁虎驱动: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)
4

2 回答 2

0

出现此错误的原因是 Python 无法直接找到函数 FirefoxBinary。

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

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

后来发现这里已经有解决方案了(间接)。

于 2016-10-31T23:53:05.980 回答
0

重新启动我的机器解决了这个问题。geckodriver.exe(如果您将其保存在其中一个位置,则可能需要PATH

不确定这是否是需要解决的真正问题,而是变量之一。


geckodriver.exe 和 Firefox 版本支持的小背景:

geckodrivergithub页面:

Firefox 47 is explicitly not supported

因此,如果要使用Firefox 47.0.1版本,请使用Firefox driver但不要使用geckodriver.

  1. 在 selenium 2.53 的情况下,您不需要做任何额外的事情(不需要设置geckodriveras selenium 2.53uses Firefox driverby default)。
  2. 在 Selenium 3.0 中,我们必须使用设置geckodriver路径(如ingeckodriverdefault驱动程序)并设置为,因此功能将被禁用并使用默认的 Firefox 驱动程序。FirefoxSelenium 3.0System.setPropertymarionettefalsegeckodriver

示例代码:

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://stackoverflow.com/a/40658421/2575259
于 2016-11-16T15:58:56.670 回答