Python Selenium报cannot find dict desiredCapabilities错误求解
问题根因
报错Selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find dict 'desiredCapabilities'的核心诱因是ChromeDriver、Chrome浏览器、Selenium库三者版本严重不匹配。你当前使用的ChromeDriver 2.24是2016年发布的远古版本,仅支持Chrome 52-54,和目前Windows 11系统上的新版Chrome、4.x版本Selenium的参数逻辑完全不兼容。另外你贴出的两段示例代码本身存在语法错误,也会导致运行异常。
可行解决方案
- 对齐核心组件版本
先打开本地Chrome的「设置-关于Chrome」查看当前Chrome的大版本号,下载和大版本完全对应的ChromeDriver,禁止使用跨大版本的老旧驱动。如果使用Selenium 4.6及以上版本,不需要手动下载ChromeDriver,Selenium内置的Selenium Manager会自动匹配、下载对应版本的驱动,直接初始化即可。如果使用Selenium Grid远程节点,需要同步对齐节点侧的Selenium、ChromeDriver、Chrome版本,旧版本Grid无法识别新版Selenium客户端的传参逻辑,同样会触发该报错。 - 修正代码语法与旧版写法问题
你贴出的代码存在引号不匹配、参数写法过时、缺少协议头、语法缺漏等问题,对应场景的正确写法如下:
本地启动Chrome的最简可运行代码:
from selenium import webdriver from selenium.webdriver.chrome.service import Service # Selenium 4.6+ 可直接初始化,无需手动指定驱动路径 driver = webdriver.Chrome() # 4.6以下版本通过Service指定驱动路径,路径前加r避免反斜杠转义 # service = Service(executable_path=r"C:\your\path\chromedriver.exe") # driver = webdriver.Chrome(service=service) driver.get('https://stackoverflow.com')
远程调用Selenium Grid的正确代码(Selenium 4已废弃直接传desired_capabilities的旧写法,统一用options传配置):
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.platform_name = "WINDOWS" # 注意该参数为浏览器版本,非系统版本 options.browser_version = "stable" driver = webdriver.Remote( command_executor="http://198.0.0.1:4444/wd/hub", options=options )
避坑提示
- Windows系统下写本地文件路径时,建议在字符串前加
r标记为原始字符串,避免反斜杠被识别为转义字符导致路径解析失败 - 远程调用Grid的地址必须带
http://或https://协议头,否则会触发连接错误 - Selenium、ChromeDriver、Chrome三者只要跨大版本不匹配,就会出现各类奇怪的启动报错,优先排查版本问题不要盲目改代码
内容的提问来源于stack exchange,提问作者Pedro H.
相关产品推荐
相关产品推荐

