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

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

问题描述:

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

There are about 100 posts about the same issue but none of them seem to work for me, hence asking again. I'm trying to launch a Firefox browser using Python and Selenium and I get the following error:

WebDriverException :消息:浏览器似乎已经退出,我们无法连接.如果您在FirefoxBinary构造函数中指定了log_file,请检查该日志以获取详细信息.

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.

我尝试了网络上的每个答案,但似乎无济于事.

I tried each and every answer on the web but nothing seems to work.

这是我的代码:

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,那么我得到的错误是

If I set caps["marionette"] = True then the error I get is

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

SessionNotCreatedException: Message: Unable to find a matching set of capabilities

我正在运行的软件版本:

Versions of software I'm running:

Firefox :62.0(64位)

Firefox: 62.0 (64 bit)

:3.14.0

壁虎:0.21.0

Python :3

操作系统:Windows 8.1 64位

OS: Windows 8.1 64 bit

任何帮助将不胜感激.

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

I've uninstalled and re-installed Firefox but didn't work. Also tried installing Firefox 61.0.2, still no luck.

此错误消息...

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浏览器会话.

...implies that the GeckoDriver was unable to initiate/spawn a new WebBrowser i.e. Firefox Browser session.

您需要注意以下几点:

  • 要设置FirefoxBinary,您需要使用FirefoxOptions(),而不是传递 geckodriver 二进制文件的绝对路径,您必须传递所需 firefox 二进制文件的绝对路径.
  • 在使用 GeckoDriver v0.21.0 时,您必须强制使用牵线木偶,因此请使其保持不变(默认为true)或设置牵线木偶 true .
  • 您自己的代码(其中包含一些小的更改)将是:

  • To set the FirefoxBinary you need to use the FirefoxOptions() and instead of passing the absolute path of geckodriver binary, you have to pass the absolute path of the desired firefox binary.
  • As you are using GeckoDriver v0.21.0 you have to mandatorily use marionette so either keep it unchanged (by default true) or set marionette to true.
  • Your own code with incorporating the minor changes will be:

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()

  • 控制台输出:

  • Console Output:

    Headless Firefox Initialized
    

  • 在这里您可以找到有关 查看更多