【问题标题】:WebDriverException: Message: The browser appears to have exited before we could connect error with GeckoDriver Selenium and PythonWebDriverException:消息:浏览器似乎已经退出,然后我们可以使用 GeckoDriver Selenium 和 Python 连接错误
【发布时间】:2019-02-18 13:37:56
【问题描述】:

大约有 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:消息:无法找到匹配的功能集

我正在运行的软件版本:

Firefox:62.0(64 位)

:3.14.0

壁虎:0.21.0

Python:3

操作系统:Windows 8.1 64 位

任何帮助将不胜感激。

编辑:我已经卸载并重新安装了 Firefox,但没有工作。也试过安装 Firefox 61.0.2,还是不行。

【问题讨论】:

  • firefox_binary 应该是您的 Firefox 可执行文件 (firefox.exe) 的路径,但不是 geckodriver.exe

标签: python-3.x selenium firefox selenium-webdriver geckodriver


【解决方案1】:

此错误消息...

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 无法启动/生成新的 WebBrowserFirefox Browser 会话。

您需要注意以下几点:

  • 要设置FirefoxBinary,你需要使用FirefoxOptions(),而不是传递geckodriver二进制文件的绝对路径,你必须传递所需 firefox 二进制文件的绝对路径
  • 当您使用 GeckoDriver v0.21.0 时,您必须强制使用 ma​​rionette,因此要么保持不变(默认为 true),要么设置 ma​​rionette 到 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
    
  • 在这里你可以找到关于Unable to find a matching set of capabilities with selenium 3.4.3, firefox 54.0 and gecko driver 0.17的详细讨论

【讨论】:

  • 我遇到了与 OP 相同的问题,当我尝试实施此解决方案时,我收到 PermissionError: [Errno 13] Permission denied: 'geckodriver.log'...
【解决方案2】:

确保(尤其是在 Windows (Win 10) 上)您的浏览器和控制器 (python/C/java/perl/etc) 都是 x64 或 win32,Microsoft 将不再在它们之间进行切换。

因此,如果您尝试从 x32 位 python 控制 64 位浏览器(默认情况下将从 firefox 下载的内容),它将在您连接之前退出.. 去安装一个 win32 版本的 firefox 以获得魔法发生

【讨论】:

    【解决方案3】:

    在尝试了不同论坛上的几乎所有答案后,一个简单的自我试验解决了这个问题,即您需要有 62 位或 32 位的 python、firefox 浏览器和 geckodriver。这不匹配导致了我的问题。

    确保您对所有 3 个组件使用相同的位版本后,只需使用以下行来运行 firefox:

    ffPath = "C:\\Drivers\\geckodriver.exe"
    os.environ["webdriver.firefox.driver"] = ffPath
    driver = webdriver.Firefox(executable_path=ffPath)
    driver.get(url)
    

    【讨论】:

      【解决方案4】:

      对我来说,问题是 python 和 gekodriver 之间的版本不匹配。一旦所有相关方都是 64 位,它就像一个魅力

      【讨论】:

      • 这将对 OP 的问题做出一个不错的评论 ;) 可能那时 OP 可以为您提供更多信息,您可以提供详细的答案。
      【解决方案5】:

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

      pip install -U selenium
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案6】:
      binary = r'C:\Program Files\Mozilla Firefox\firefox.exe'
      options = Options()
      options.binary = binary
      browser = webdriver.Firefox(firefox_options=options, executable_path=r"C:\Drivers\geckodriver.exe")
      

      【讨论】:

      • 虽然这段代码 sn-p 可以解决问题,including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
      猜你喜欢
      • 2014-11-22
      • 2013-09-20
      • 2021-11-14
      • 2015-01-31
      • 2016-10-30
      • 2016-10-16
      • 2021-12-26
      • 1970-01-01
      • 2019-03-03
      相关资源
      最近更新 更多