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

如何阻止SeleniumWire默认跳转至HTTPS,强制使用HTTP?

解决SeleniumWire+ChromeDriver强制保留HTTP协议的方案

以下几种方法可以阻止Chrome自动将HTTP重定向到HTTPS:

1. 禁用Chrome的HSTS及相关安全设置

Chrome的HSTS(HTTP严格传输安全)机制会自动将HTTP请求跳转至HTTPS,通过添加启动参数可以禁用该功能:

from seleniumwire import webdriver

options = webdriver.ChromeOptions()
# 禁用HSTS缓存,避免强制跳转HTTPS
options.add_argument('--disable-hsts')
# 忽略HTTPS证书错误(如果目标站点HTTPS证书无效)
options.add_argument('--ignore-certificate-errors')
# 禁用安全浏览的自动更新,防止触发HTTPS强制升级
options.add_argument('--safebrowsing-disable-auto-update')

driver = webdriver.Chrome(options=options)
driver.get('http://somesite.com:port/')

2. 用SeleniumWire拦截并重写重定向响应

通过SeleniumWire的请求拦截器,捕获3xx重定向响应,将Location头中的HTTPS替换为HTTP,直接阻止跳转:

from seleniumwire import webdriver

def intercept_redirect(request):
    if request.response and 300 <= request.response.status_code < 400:
        location_header = request.response.headers.get('Location')
        if location_header and location_header.startswith('https://'):
            # 将重定向目标替换为HTTP协议
            request.response.headers['Location'] = location_header.replace('https://', 'http://')
            # 也可以直接将响应状态码改为200,彻底终止重定向流程
            # request.response.status_code = 200

options = webdriver.ChromeOptions()
driver = webdriver.Chrome(options=options)
# 绑定请求拦截器
driver.request_interceptor = intercept_redirect

driver.get('http://somesite.com:port/')

3. 修改Chrome偏好设置关闭自动HTTPS升级

通过Chrome的实验性偏好设置,直接关闭自动HTTPS升级功能:

from seleniumwire import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
    'profile.default_content_setting_values.automatic_https_upgrades': False
})

driver = webdriver.Chrome(options=options)
driver.get('http://somesite.com:port/')

如果单个方法无效,可以尝试组合使用,比如同时禁用HSTS+关闭自动HTTPS升级。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 01:55:09