大约有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 二进制文件的绝对路径。 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
关于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/