WebDriverException:消息:浏览器似乎已经退出,然后我们才能使用 GeckoDriver Selenium 和 Python 连接错误

Ani*_*ish 9 firefox selenium python-3.x selenium-webdriver geckodriver

大约有 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()
Run Code Online (Sandbox Code Playgroud)

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

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

我正在运行的软件版本:

火狐:62.0(64 位)

:3.14.0

壁虎:0.21.0

蟒蛇:3

操作系统:Windows 8.1 64 位

任何帮助将不胜感激。

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

Deb*_*anB 10

这个错误信息...

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.
Run Code Online (Sandbox Code Playgroud)

...暗示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()
    
    Run Code Online (Sandbox Code Playgroud)
  • 控制台输出:

    Headless Firefox Initialized
    
    Run Code Online (Sandbox Code Playgroud)
  • 您可以在此处找到有关无法找到与 selenium 3.4.3、firefox 54.0 和 gecko 驱动程序 0.17 匹配的功能集的详细讨论