求助:Selenium Manager无法自动下载ChromeDriver,求自动解决方案
解决Selenium自动下载ChromeDriver失败问题
问题描述
我正在学习Selenium,但基础脚本无法运行。Chrome版本为117.0.5938.150,按说Chrome 114+版本Selenium会自动匹配下载对应ChromeDriver,但这个功能失效了,不想手动下载,求自动解决方法。
测试代码
from selenium import webdriver driver = webdriver.Chrome() driver.get("https://www.selenium.dev/selenium/web/web-form.html") driver.quit()
报错信息
Traceback (most recent call last): File "C:\Program Files\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 38, in get_path path = SeleniumManager().driver_location(options) if path is None else path ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\Python312\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 98, in driver_location output = self.run(args) ^^^^^^^^^^^^^^ File "C:\Program Files\Python312\Lib\site-packages\selenium\webdriver\common\selenium_manager.py", line 144, in run raise WebDriverException(f"Unsuccessful command executed: {command}.\n{result}{stderr}") selenium.common.exceptions.WebDriverException: Message: Unsuccessful command executed: C:\Program Files\Python312\Lib\site-packages\selenium\webdriver\common\windows\selenium-manager.exe --browser chrome --output json. {'code': 65, 'message': 'error sending request for url (https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/117.0.5938.149/win64/chromedriver-win64.zip): error trying to connect: tcp connect error: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (os error 10060)', 'driver_path': '', 'browser_path': ''} The above exception was the direct cause of the following exception: Traceback (most recent call last): File "m:\.socials\CS Lang. n Technologies\Python\email_tracker_project\main.py", line 6, in <module> driver = webdriver.Chrome(service=service, options=options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\Python312\Lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 45, in __init__ super().__init__( File "C:\Program Files\Python312\Lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 51, in __init__ self.service.path = DriverFinder.get_path(self.service, options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\Python312\Lib\site-packages\selenium\webdriver\common\driver_finder.py", line 41, in get_path raise NoSuchDriverException(msg) from err selenium.common.exceptions.NoSuchDriverException: Message: Unable to obtain driver for chrome using Selenium Manager.; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location
自动解决方法
从报错信息看,核心问题是Selenium Manager无法连接谷歌的Driver下载服务器,以下是几个自动解决的方案:
配置网络代理:如果你的网络需要代理才能访问外网,在代码开头添加代理环境变量配置:
import os # 替换成你的代理地址和端口 os.environ['HTTP_PROXY'] = 'http://proxy.example.com:8080' os.environ['HTTPS_PROXY'] = 'http://proxy.example.com:8080'升级Selenium到最新版:旧版本的Selenium Manager可能存在网络兼容问题,执行命令更新:
pip install --upgrade selenium切换国内镜像源:通过设置环境变量,让Selenium Manager从国内CDN下载ChromeDriver,避免连接谷歌服务器超时。在Windows命令行执行:
setx SE_MANAGER_GOOGLE_CDN "https://registry.npmmirror.com/-/binary/chromedriver"执行后重启VSCode再运行脚本即可。
检查防火墙/杀毒软件:临时关闭系统防火墙或杀毒软件,确认是否是它们阻断了Selenium Manager的网络请求,后续可将
selenium-manager.exe加入白名单。
内容的提问来源于stack exchange,提问作者Muhammad Musa Khan
相关产品推荐
相关产品推荐

