gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:30:44 25 4
gpt4 key购买 nike

大约有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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com