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

如何修复(而非忽略)Selenium(Chrome)中的SSL证书错误

解决Chrome 115+下Selenium Wire Undetected Chromedriver的全局SSL证书错误

问题

Chrome升级至115版本后,使用Selenium Wire的Undetected Chromedriver访问任何HTTPS网站都会触发SSL证书错误。已知--ignore-certificate-errors可临时绕过,但需彻底修复以保障连接安全。

代码示例:

import time

from seleniumwire import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options

options = Options()
# options.add_argument('--ignore-certificate-errors')
driver = uc.Chrome(options=options)
driver.get('https://google.com')
time.sleep(100)

核心原因

  1. Selenium Wire代理机制限制:Selenium Wire通过内置代理拦截HTTPS流量,需要其根证书被Chrome信任。Chrome 115+调整了证书信任逻辑,旧版本的自动证书注入流程失效。
  2. Undetected Chromedriver环境隔离:默认创建干净的临时用户目录,不继承系统或常规Chrome的信任证书列表,导致Selenium Wire的根证书未被识别。

修复方案

方案1:手动信任Selenium Wire根证书(最稳定)

  1. 导出根证书:
    运行以下代码生成ca.crt文件:
    from seleniumwire import webdriver
    
    driver = webdriver.Chrome()
    with open('ca.crt', 'wb') as f:
        f.write(driver.proxy.cert)
    driver.quit()
    
  2. 创建Chrome专用配置文件:
    • 打开Chrome,新建用户配置文件(如命名为selenium_secure)
    • 导入证书:设置 → 隐私和安全 → 安全 → 管理证书 → 受信任的根证书颁发机构 → 导入ca.crt并完成向导
  3. 代码中指定配置文件:
    import time
    from seleniumwire import undetected_chromedriver as uc
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    # 替换为你的配置文件实际路径
    options.add_argument(r'--user-data-dir=C:\Users\YourName\AppData\Local\Google\Chrome\User Data\selenium_secure')
    driver = uc.Chrome(options=options)
    driver.get('https://google.com')
    time.sleep(100)
    

方案2:自动注入证书到临时目录(适合自动化场景)

通过脚本自动将证书导入Undetected Chromedriver的临时用户目录(以Windows为例):

import time
import os
import subprocess
from seleniumwire import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options
from seleniumwire import webdriver as wire_driver

# 导出Selenium Wire根证书
temp_driver = wire_driver.Chrome()
ca_cert = temp_driver.proxy.cert
temp_driver.quit()

# 临时保存证书
cert_path = './temp_ca.crt'
with open(cert_path, 'wb') as f:
    f.write(ca_cert)

# 创建临时用户目录
profile_dir = './selenium_temp_profile'
os.makedirs(profile_dir, exist_ok=True)

# 将证书导入当前用户的根证书存储
subprocess.run(
    ['certutil', '-addstore', '-user', 'Root', cert_path],
    check=True,
    stdout=subprocess.DEVNULL,
    stderr=subprocess.DEVNULL
)
os.remove(cert_path)

# 启动浏览器并指定临时目录
options = Options()
options.add_argument(f'--user-data-dir={profile_dir}')
driver = uc.Chrome(options=options)
driver.get('https://google.com')
time.sleep(100)

# 清理临时资源
driver.quit()
import shutil
shutil.rmtree(profile_dir)

方案3:升级依赖包(快速尝试)

Selenium Wire和Undetected Chromedriver可能已针对Chrome 115+修复该问题,执行升级:

pip install --upgrade selenium-wire undetected-chromedriver

升级后直接运行原代码,若错误消失则无需额外配置。

验证

访问HTTPS网站后,地址栏显示安全锁图标,无SSL错误提示,即为修复成功。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 01:20:27