预期的浏览器二进制位置,但无法在默认位置找到二进制文件,没有使用 GeckoDriver 提供的 'moz:firefoxOptions.binary' 功能

Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided using GeckoDriver

from selenium import webdriver;
browser= webdriver.Firefox();
browser.get('http://www.seleniumhq.org');

当我尝试 运行 这段代码时,它给了我一个 error 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.

任何想法 - 非常感谢!

您需要下载 geckodriver。

https://github.com/mozilla/geckodriver/releases

from selenium import webdriver;

browser= webdriver.Firefox('./geckodriver');
browser.get('http://www.seleniumhq.org');

您应该从 https://github.com/mozilla/geckodriver/releases 下载合适的网络驱动程序并将其放入您的 py 文件所在的文件夹中。你也可以把它放在任何地方,只要它在你的系统路径中的位置。

Selenium 使用 网络驱动程序(每个网络浏览器都有一个特定的驱动程序)以便与系统上安装的浏览器进行通信 (你的情况是 Firefox)。

要使用 Firefox,您必须:

  1. 从以下位置下载其网络驱动程序 https://github.com/mozilla/geckodriver/releases
  2. 将网络驱动程序放在文件系统中的特定位置(例如与python脚本相同的文件夹)
  3. 在python代码初始化时添加web驱动位置路径

所以最终的代码应该是这样的:

from selenium import webdriver

browser = webdriver.Firefox('./geckodriver')

browser.get('https://www.python.org/')

注意:有时 较新版本 的网络驱动程序与系统上安装的旧版本浏览器不兼容 .

这个错误信息...

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.

...意味着 GeckoDriver 无法在默认位置找到 Firefox 二进制文件。此外,您还没有通过 moz:firefoxOptions.binary 能力。


解决方案

可能在您的系统中 安装在自定义位置,这些情况下您需要通过 Firefox[=] 的 绝对路径 40=]二进制通过moz:firefoxOptions.binary能力如下:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(executable_path=r'C:\WebDrivers\geckodriver.exe', options=options)
driver.get('http://google.com/')

参考资料

您可以在以下位置找到一些相关的详细讨论:

  • SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary'
  • InvalidArgumentException: Message: binary is not a Firefox executable error using GeckoDriver Firefox Selenium and Python

我已经卸载 firefox 并重新安装它解决了我的问题。

我的系统上根本没有安装 Firefox。这就是出现此错误的原因。

同样的问题:

  • 环境
    • OS: Mac
      • 未安装Firefox应用程序
      • 已安装geckodriver,可在PATH
      • 中找到
  • 错误原因:未安装Firefox
  • 解决方案:(转到 firefox official site 下载并安装 Firefox

在此之前,请确保路径变量包含 geckodriver click here to download driver 和 运行 在 python 脚本下面。

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
driver = webdriver.Firefox(options=options)
driver.get('http://google.com/')

我遇到了同样的问题(Windows,Firefox v99,Selenium 4.1.4,geckodriver 0.31.0),exe文件的路径和驱动程序初始化设置正确,通过更改解决了问题win32 by win64 版本的 geckodriver