Selenium启动Chrome浏览器报错求助(Python/PyCharm环境)
解决Selenium打开Chrome报错的实用方案
十有八九是Selenium版本和Chrome版本不匹配,或者你用了旧版Selenium的写法导致的,按下面的步骤来:
1. 先确认版本对应
- 查Chrome版本:打开Chrome→右上角三点→帮助→关于Google Chrome,记下版本号(比如126.0.6478.127)
- 升级Selenium到4.x以上:在PyCharm终端执行
pip show selenium,如果版本低于4.0,再执行pip install --upgrade selenium
2. 用Selenium 4.x的正确写法
Selenium 4.6+自带了驱动自动管理工具,不用手动下载ChromeDriver,也不用webdriver_manager,代码直接这么写:
from selenium import webdriver # 直接初始化浏览器,自动匹配对应驱动 driver = webdriver.Chrome() driver.get("https://www.google.com") # 用完记得关闭浏览器 driver.quit()
如果你的Selenium版本在4.6到4.9之间,需要加Service类:
from selenium import webdriver from selenium.webdriver.chrome.service import Service driver = webdriver.Chrome(service=Service()) driver.get("https://www.google.com") driver.quit()
3. 排查PyCharm环境问题
- 确认PyCharm用的是安装了Selenium的Python解释器:依次点击
File→Settings→Project: xxx→Python Interpreter,查看列表里是否有selenium,且版本符合要求 - 如果用了虚拟环境,先激活环境再安装:Windows终端执行
venv\Scripts\activate,Mac/Linux终端执行source venv/bin/activate,之后再执行pip install selenium --upgrade
4. 国内网络手动补驱动的方案
如果自动下载驱动失败,手动操作:
- 下载和Chrome版本完全一致的ChromeDriver(找国内靠谱的镜像站点下载)
- 把驱动文件放到Python安装目录的Scripts文件夹里,或者在代码里直接指定路径:
from selenium import webdriver from selenium.webdriver.chrome.service import Service # 替换成你的chromedriver实际路径,Windows注意用双反斜杠或单斜杠 service = Service(executable_path="C:/Python/Scripts/chromedriver.exe") driver = webdriver.Chrome(service=service) driver.get("https://www.google.com") driver.quit()
内容的提问来源于stack exchange,提问作者roi
相关产品推荐
相关产品推荐

