python-3.x - WebDriverException : Message: The browser appears to have exited before we could connect error with GeckoDriver Selenium and Python

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

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

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.



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

这是我的代码:
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: Message: Unable to find a matching set of capabilities



我正在运行的软件版本:

Firefox :62.0(64位)

Selenium :3.14.0

壁虎:0.21.0

Python :3

操作系统:Windows 8.1 64位

任何帮助将不胜感激。

编辑:我已经卸载并重新安装了Firefox,但是没有用。还尝试安装Firefox 61.0.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 无法启动/产生新的 Web浏览器,即 Firefox浏览器 session 。

您需要注意以下几点:
  • 要设置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
    
  • 在这里您可以找到有关Unable to find a matching set of capabilities with selenium 3.4.3, firefox 54.0 and gecko driver 0.17的详细讨论
  • 关于python-3.x - WebDriverException : Message: The browser appears to have exited before we could connect error with GeckoDriver Selenium and Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52320131/

    相关文章:

    python - Selenium webdriver 不会完全加载页面(python)

    python - 计算 pandas df 中的非空值

    java - 如何在Java(Selenium)中从十进制数中删除不需要的零。但是如果最后一位数字不为0,那么应该显示它

    python - 如何在Windows身份验证中传递URL中的特殊字符?

    javascript - Firefox 不会动画化 SVG 大小的变化

    html - 浏览器字体大小如何决定我们的应用程序呈现方式

    python - 无法绘制一列相对于其他列的热图

    python - 如何比较两个 DF 中的两列并保持一些列常量并打印行?

    java - WebDriver中的webElement有多少种点击方式?

    html - Android Firefox 上的字体更大