WebDriverException:消息:浏览器似乎在我们可以连接错误之前退出 GeckoDriver Selenium 和 Python

新手上路,请多包涵

大约有 100 篇关于同一问题的帖子,但似乎没有一篇对我有用,因此再次询问。我正在尝试使用 Python 和 Selenium 启动 Firefox 浏览器,但出现以下错误:

WebDriverException :消息:浏览器似乎在我们可以连接之前已经退出。如果您在 FirefoxBinary 构造函数中指定了 log_file,请检查它以获取详细信息。

我尝试了网上的每一个答案,但似乎没有任何效果。

这是我的代码:

 from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities.FIREFOX
caps["marionette"] = False

binary = FirefoxBinary('d:\\Desktop\\IEDriver\\geckodriver.exe')

options = Options()
options.set_headless(headless=True)
driver = webdriver.Firefox(firefox_binary=binary, firefox_options=options, executable_path=r'd:\\Desktop\\IEDriver\\geckodriver.exe')
driver.get("http://google.com/")
print ("Headless Firefox Initialized")
driver.quit()

如果我设置 caps["marionette"] = True 那么我得到的错误是

SessionNotCreatedException :消息:无法找到匹配的功能集

我正在运行的软件版本:

火狐浏览器:62.0(64 位)

:3.14.0

壁虎:0.21.0

蟒蛇:3

操作系统:Windows 8.1 64 位

任何帮助将不胜感激。

编辑:我已经卸载并重新安装了 Firefox,但没有用。还尝试安装 Firefox 61.0.2,仍然没有成功。

原文由 Anish 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 887
2 个回答

这个错误信息…

 WebDriverException: Message: The browser appears to have exited before we could connect.
If you specified a log_file in the FirefoxBinary constructor, check it for details.

…暗示 GeckoDriver 无法启动/生成新的 WebBrowser ,即 Firefox 浏览器 会话。

您需要处理以下几件事:

  • 要设置 FirefoxBinary 您需要使用 FirefoxOptions() 而不是传递 geckodriver 二进制文件的 _绝对路径_,您必须传递所需的 firefox 二进制文件的 _绝对路径_。
  • 当您使用 GeckoDriver v0.21.0 时,您必须强制使用 木偶,因此要么保持不变(默认 true ),要么将 木偶 设置为 true
  • 您自己的代码包含较小的更改将是:
   from selenium import webdriver
  from selenium.webdriver.firefox.options import Options
  from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

  binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
  options = Options()
  options.set_headless(headless=True)
  options.binary = binary
  cap = DesiredCapabilities().FIREFOX
  cap["marionette"] = True #optional
  driver = webdriver.Firefox(firefox_options=options, capabilities=cap, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
  driver.get("http://google.com/")
  print ("Headless Firefox Initialized")
  driver.quit()

  • 控制台输出:
   Headless Firefox Initialized

原文由 undetected Selenium 发布,翻译遵循 CC BY-SA 4.0 许可协议

这个错误让我很困扰。安装这个:

 pip install -U selenium

原文由 Abdullah Khan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进