Selenium+webdriver_manager遇WebDriverException连接服务失败求助
解决ChromeDriver连接失败问题
替代方案:使用Selenium自带的SeleniumManager
Selenium 4.6及以上版本内置了SeleniumManager工具,可自动匹配并管理ChromeDriver,无需再依赖webdriver_manager,简化后的代码如下:
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--no-sandbox') # 禁用沙箱模式 options.add_argument('--headless') # 无头模式运行(无GUI) options.binary_location="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" driver = webdriver.Chrome(options=options)
额外排查思路(针对已做基础排查仍报错的情况)
- 适配ARM架构Mac的无头模式:M1/M2芯片Mac运行无头Chrome时,需添加额外参数:
--disable-gpu和--window-size=1920,1080,同时确认Chrome为ARM原生版本(通过Chrome菜单→关于Google Chrome查看架构) - 手动测试ChromeDriver:直接下载对应ARM架构的ChromeDriver,手动指定路径运行代码,同时在终端执行
/path/to/chromedriver,查看是否有系统级报错(如权限、兼容性提示)from selenium.webdriver.chrome.service import Service as ChromeService driver = webdriver.Chrome( service=ChromeService("/Users/abc/.wdm/drivers/chromedriver/mac64/115.0.5790.114/chromedriver-mac-arm64/chromedriver"), options=options ) - 检查系统安全设置:Mac可能阻止未签名的ChromeDriver运行,前往「系统设置→隐私与安全性」,若有chromedriver相关提示,点击「允许」
- 清理缓存目录:删除
/Users/abc/.wdm目录,清除旧驱动缓存后重新运行代码 - 降级Chrome版本:若新版本存在适配问题,暂时降级到114.x等稳定版本测试
内容的提问来源于stack exchange,提问作者mikelowry
相关产品推荐
相关产品推荐

