You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chromedriver与Chrome无法配合Selenium正常运行问题求助

解决Selenium启动Chrome无响应的问题

针对你遇到的Chrome窗口无法弹出的情况,可按以下步骤排查:

1. 改用Selenium 4推荐的Service类初始化驱动

旧的webdriver.Chrome()写法在部分环境下可能存在兼容性问题,试试显式指定驱动路径并使用Service类:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

site = 'https://www.google.com'
# 可直接写文件名(若已在环境变量路径中)或完整路径,比如C:\PythonXX\Scripts\chromedriver.exe
service = Service(executable_path='chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.get(site)

2. 清理后台残留进程

打开任务管理器,结束所有chrome.exe和chromedriver.exe进程后重新运行代码——残留进程可能导致新驱动无法正常启动。

3. 添加Chrome启动参数强制显示窗口

尝试添加启动参数确保窗口正常弹出,比如:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--start-maximized")  # 最大化窗口
options.add_argument("--disable-gpu")      # 禁用GPU加速(部分环境下可解决显示问题)
options.add_argument("--no-sandbox")       # 禁用沙箱模式(适配部分系统环境)

service = Service(executable_path='chromedriver.exe')
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://www.google.com')

4. 捕获异常查看具体错误

在代码中添加异常捕获,获取详细报错信息,这能帮你定位根本问题:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

try:
    site = 'https://www.google.com'
    service = Service(executable_path='chromedriver.exe')
    driver = webdriver.Chrome(service=service)
    driver.get(site)
except Exception as e:
    print(f"错误信息:{str(e)}")

5. 检查Python环境一致性

确认当前运行代码的Python环境,和你放置chromedriver的Scripts目录所属的Python版本一致——如果切换到了虚拟环境,需要将chromedriver复制到虚拟环境的Scripts目录下。

内容的提问来源于stack exchange,提问作者ashtrobe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 02:12:35