Windows环境下使用VPN时Python Selenium启动ChromeDriver报错咨询
Windows 环境 Selenium 调用 ChromeDriver 连接 Cyberghost VPN 时报错处理
故障基本特征
- 触发条件:仅连接 Cyberghost VPN 时问题复现,断开 VPN 后脚本可正常运行
- 平台差异:问题仅在 Windows 系统下出现,macOS 环境运行完全正常
- 运行表现:执行 Python 启动脚本后,Chrome 浏览器打开卡顿约 20 秒后自动关闭,抛出会话创建失败的异常
- 已完成排查:已确认本地 ChromeDriver 与 Chrome 87.0.4280.66 版本完全匹配,现有公开资料未匹配到对应场景的有效解决路径
- 约束条件:脚本依赖的定位扩展仅支持 Chrome 浏览器,Firefox 无对应适配版本,无法切换浏览器规避问题
完整报错堆栈
Traceback (most recent call last): File "myscript.py", line 461, in <module> File "C:\Users\ickoi\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__ RemoteWebDriver.__init__( File "C:\Users\ickoi\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__ self.start_session(capabilities, browser_profile) File "C:\Users\ickoi\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Users\ickoi\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute self.error_handler.check_response(response) File "C:\Users\ickoi\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.SessionNotCreatedException: Message: session not created from disconnected: unable to connect to renderer (Session info: chrome=87.0.4280.66)
待运行的脚本核心代码
from selenium import webdriver from time import sleep from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select options = webdriver.ChromeOptions() options.add_extension('./location2.crx') # 本地存储的定位相关CRX扩展 options.add_experimental_option("prefs", { "profile.default_content_setting_values.geolocation": 1, "profile.default_content_setting_values.notifications": 1 }) options.add_experimental_option('excludeSwitches', ['enable-logging']) driver = webdriver.Chrome(options=options)
可执行修复方案
该问题的本质是 Cyberghost VPN Windows 端安装的虚拟网卡、网络过滤驱动拦截了 Chrome 主进程与渲染进程之间的本地回环通信,导致渲染进程无法建立连接,可按以下顺序调整修复:
- 在 ChromeOptions 配置中追加沙箱关闭参数:
options.add_argument('--no-sandbox'),跳过沙箱机制对本地跨进程连接的拦截规则 - 在 ChromeOptions 配置中追加进程隔离关闭参数:
options.add_argument('--disable-process-isolation'),避免虚拟网卡过滤规则拦截跨进程的本地回环请求 - 在 Cyberghost VPN 的规则配置中,将
127.0.0.0/8本地回环地址段加入直连白名单,确保 Selenium、ChromeDriver、Chrome 三者之间的本地通信流量不被 VPN 代理转发 - 调整扩展加载逻辑:将启动时预加载本地 CRX 扩展的方式,替换为启动无扩展的纯净 Chrome 进程后手动加载扩展,避免 VPN 网络环境下扩展初始化超时触发渲染进程崩溃
- 为 Selenium 启动的 Chrome 配置独立临时用户目录:追加参数
options.add_argument('--user-data-dir=C:\\temp\\chrome_selenium_profile'),避免原有浏览器配置中残留的 VPN 代理规则干扰启动流程
内容的提问来源于stack exchange,提问作者R. Rad
相关产品推荐
相关产品推荐

