WebDriverException:消息:无法加载配置文件。可能的firefox版本不匹配。您必须将GeckoDriver用于Firefox48+和Selenium

2024-04-27 04:52:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图follow an example如何通过python和selenium解析网站。 但是我总是遇到以下问题:调用函数webdriver.Firefox 打开一个firefox实例,但无法通过get调用任何网站,这似乎是:整个代码在功能firefox中被阻塞(请参阅:print(“open call never Reach”))浏览器正在打开,大约30秒后,一个异常导致broswer退出,并显示以下消息:

selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmpl5dm_azd If you specified a log_file in the FirefoxBinary constructor, check it for details

那么我错在哪里呢?我如何设置正确的配置文件? 我试图将木偶模式设置为True,但出现错误:“无法找到匹配的功能集”

from selenium.webdriver import Firefox
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False

options = Options()
options.log.level = "trace"
options.headless = True

binary = FirefoxBinary("/usr/bin/firefox")
pathDriver = "./geckodriver" 
testUrl="https://duckduckgo.com/"

print("will create firefox instance")
browser = webdriver.Firefox(firefox_binary=binary,options=options,capabilities=cap,executable_path=pathDriver)
print("open call never reached")
browser.get(testUrl)

webdriver.quit()

我的测试环境:

$ name -a
Linux 5.5.0-0.bpo.2-amd64 #1 SMP Debian 5.5.17-1~bpo10+1 (2020-04-23) x86_64 GNU/Linux

我还下载了最新的selenium和geckodriver 以下是我使用的版本:

$ python3 –version
Python 3.7.3
$ pip3 freeze | grep sel
selenium==3.141.0
$ geckodriver -V
geckodriver 0.27.0 (7b8c4f32cdde 2020-07-28 18:16 +0000)
$ which firefox 
/usr/bin/firefox
$ firefox -v
Mozilla Firefox 68.10.0esr

Tags: fromimportget网站seleniumcommonfirefoxoptions
2条回答

使用GeckoDriver启动/生成新的浏览上下文时,即使用Firefox 48+版本的Firefox浏览器会话时,必须强制使用Marionette


解决方案

解决方案是使用默认设置或将marionette设置为True,如下所示:

cap = DesiredCapabilities().FIREFOX
cap["marionette"] = True

您为DesiredCapabilities添加了括号

cap = DesiredCapabilities.FIREFOX
cap['marionette'] = False 

或者,您可以使用webdriver_管理器库,这将有助于消除许多令人头痛的问题

pip install webdriver_manager

像这样使用它

from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver import DesiredCapabilities

options = webdriver.FirefoxOptions()
options.log.level = "trace"
options.headless = True

capabilities = DesiredCapabilities.FIREFOX
capabilities["marionette"] = False

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), options=options)

此设置可帮助您获得selenium的最新浏览器版本,错误可能是由版本不匹配引起的

相关问题 更多 >