当前位置: 代码网 > it编程>前端脚本>Python > Python Selenium中忽略证书错误的完整方法

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

2025年11月03日 Python 我要评论
之前一篇博客《python selenium 搜索和点击》会出现ssl证书错误:[502260:506160:0626/153236.677:error:net\socket\ssl_client_s

之前一篇博客《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忽略证书错误内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com