Python Selenium中忽略证书错误的完整方法

 更新时间:2025年11月03日 10:08:05   作者:Humbunklung  
这篇文章主要为大家详细介绍了Python Selenium中忽略证书错误的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

之前一篇博客《Python Selenium 搜索和点击》会出现SSL证书错误:

[502260:506160:0626/153236.677:ERROR:net\socket\ssl_client_socket_impl.cc:878] handshake failed; returned -1, SSL error code 1, net_error -103

我们可以通过浏览器选项忽略该错误。

一、忽略 SSL 证书错误

当访问使用自签名证书过期证书的 HTTPS 网站时,可通过以下配置忽略浏览器警告:

1.基础配置(适用于 Chrome/Firefox)

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

chrome_options = Options()
# 忽略所有证书错误
chrome_options.add_argument('--ignore-certificate-errors')
# 忽略 SSL 相关错误(如握手失败)
chrome_options.add_argument('--ignore-ssl-errors')

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://example.com")

2.高级场景:指定证书指纹

若需信任特定证书(如内部 CA),可添加指纹验证:

chrome_options.add_argument('--ignore-certificate-errors-spki-list=<your_cert_fingerprint>')
chrome_options.add_argument('--ca-certs=path/to/ca.pem')  # 指定证书路径

获取指纹命令openssl x509 -in ca.pem -pubkey -noout | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | openssl base64

3.浏览器兼容方案

Firefox

options = webdriver.FirefoxOptions()
options.accept_untrusted_certs = True

IE

caps = webdriver.DesiredCapabilities.INTERNETEXPLORER
caps['acceptSslCerts'] = True

二、隐藏 DevTools 监听提示

DevTools listening on ws://... 是 Chrome 的调试端口信息,可通过以下方式屏蔽:

1.禁用控制台日志输出

chrome_options.add_argument('--log-level=3')  # 关闭所有非致命日志
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])  # 禁止 Selenium 自身日志

2.移除浏览器界面提示

# 隐藏 "Chrome 正受到自动测试软件控制" 提示栏
chrome_options.add_argument('--disable-infobars')
# 禁用自动化控制特征(减少被检测风险)
chrome_options.add_argument('--disable-blink-features=AutomationControlled') 
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

3.无头模式优化

若使用无头模式,需额外关闭沙箱和 GPU:

chrome_options.add_argument('--headless=new')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')

三、完整代码示例

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

def init_driver():
    chrome_options = Options()
    # 忽略证书错误
    chrome_options.add_argument('--ignore-certificate-errors')
    chrome_options.add_argument('--ignore-ssl-errors')
    # 隐藏 DevTools 提示
    chrome_options.add_argument('--log-level=3')
    chrome_options.add_argument('--disable-infobars')
    chrome_options.add_argument('--disable-blink-features=AutomationControlled')
    chrome_options.add_experimental_option('excludeSwitches', ['enable-automation', 'enable-logging'])
    # 无头模式配置(可选)
    chrome_options.add_argument('--headless=new')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-gpu')
    
    return webdriver.Chrome(options=chrome_options)

driver = init_driver()
driver.get("https://example.com")
print("页面标题:", driver.title)
driver.quit()

四、注意事项

安全风险:忽略证书错误仅限测试环境,生产环境需使用有效证书。

反检测策略:部分网站(如 Cloudflare)会检测自动化特征,可结合 debuggerAddress 复用已有浏览器会话。

Selenium 4 兼容性:若 accept_insecure_certs=True 失效,优先使用 add_argument 参数。

通过上述配置,既可解决证书验证问题,又能保持控制台输出简洁,适用于爬虫、自动化测试等场景。

到此这篇关于Python Selenium中忽略证书错误的完整方法的文章就介绍到这了,更多相关Python Selenium忽略证书错误内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python编程学习之如何判断3个数的大小

    Python编程学习之如何判断3个数的大小

    这篇文章主要给大家介绍了关于Python编程学习之如何判断3个数的大小的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • python列表切片和嵌套列表取值操作详解

    python列表切片和嵌套列表取值操作详解

    今天小编就为大家分享一篇python列表切片和嵌套列表取值操作详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • python3.6.3安装图文教程 TensorFlow安装配置方法

    python3.6.3安装图文教程 TensorFlow安装配置方法

    这篇文章主要为大家详细介绍了python3.6.3及TensorFlow安装配置方法图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-09-09
  • Python 异步之在 Asyncio中如何运行阻塞任务详解

    Python 异步之在 Asyncio中如何运行阻塞任务详解

    这篇文章主要为大家介绍了Python 异步之在 Asyncio 中运行阻塞任务示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • django数据库自动重连的方法实例

    django数据库自动重连的方法实例

    这篇文章主要给大家介绍了关于django数据库自动重连的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用django具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-07-07
  • Python函数关键字参数及用法详解

    Python函数关键字参数及用法详解

    本文主要介绍了Python函数关键字参数及用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • 删除pandas中产生Unnamed:0列的操作

    删除pandas中产生Unnamed:0列的操作

    这篇文章主要介绍了删除pandas中产生Unnamed:0列的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Python 调用函数时检查参数的类型是否合规的实现代码

    Python 调用函数时检查参数的类型是否合规的实现代码

    这篇文章主要介绍了Python 调用函数时检查参数的类型是否合规的实现代码,本文给大家讲解的非常详细,需要的朋友可以参考下
    2024-06-06
  • python获取命令行参数实例方法讲解

    python获取命令行参数实例方法讲解

    在本篇文章里小编给大家整理的是一篇关于python获取命令行参数实例方法讲解内容,有兴趣的朋友们可以学习下。
    2020-11-11
  • 浅析Python的Django框架中的Memcached

    浅析Python的Django框架中的Memcached

    这篇文章主要介绍了浅析Python的Django框架中的缓存机制,其中着重讲到了Memcached,需要的朋友可以参考下
    2015-07-07

最新评论