跳转到主要内容
指南

selenium-wire 在 Cloudflare 上失效:undetected 模式为何被识别,以及该换成什么

NSLSolver 团队发布于 更新于 9 分钟阅读

selenium-wire 即使配合 undetected-chromedriver 也会被 Cloudflare 识别,原因在于它的工作方式。为了捕获 HTTPS 流量,它以代理身份夹在中间,解密浏览器的连接,再自己向网站建立一条新连接,于是 Cloudflare 看到的是 selenium-wire 的 TLS 握手,而不是 Chrome 的。作者在 issue #577 中确认了这一点:正是在这次上游握手中,Cloudflare 看到了 Selenium Wire。

此外,该项目已于 2024-01-03 归档(“Selenium Wire is no longer being maintained”),新版 Python 和依赖都会让它安装失败。本文介绍仍然有效的修复(固定依赖版本、disable_capture、证书与代理报错),说明 selenium-wire 各项用途的替代方案,也会讲到同样有问题的 requests-html。文中信息来自各仓库、PyPI 和 issue,核对日期 2026-12-01。

快速答案:按报错找到对应修复

selenium-wire 的问题分两类:在新版 Python 上安装失败,以及被检测。在下面找到你的那一条。

ModuleNotFoundError: No module named 'blinker._saferef'
blinker 1.8(2024 年 4 月)移除了 selenium-wire 内置 mitmproxy 所引用的模块。 pip install "blinker==1.7.0"。
ModuleNotFoundError: No module named 'pkg_resources'
selenium-wire 引用了 pkg_resources;Python 3.12 的 venv 不再预装 setuptools,而 setuptools 82 移除了 pkg_resources。 pip install "setuptools<82"。
ModuleNotFoundError: No module named 'distutils'
undetected-chromedriver 3.5.5 引用了 Python 3.12 已移除的 distutils。 安装 setuptools(它提供 distutils 兼容层),或者不再使用 undetected-chromedriver。
原版 undetected-chromedriver 能过 Cloudflare,seleniumwire.undetected_chromedriver 却不行
selenium-wire 的上游 TLS 握手被 Cloudflare 看到了(issue #577)。 开启 disable_capture,或者弃用 selenium-wire,改用不解密流量的代理方式。
地址栏显示“Your connection is not private”/“Not Secure”
页面由 selenium-wire 自己的根证书签名(issue #685、#120)。 开启拦截时这是预期行为;如果必须保持捕获,请安装它的 CA 证书。
net::ERR_PROXY_CONNECTION_FAILED
Chrome 连不上 selenium-wire 的本地代理,或者 selenium-wire 连不上你的上游代理(issue #470、#425、#719)。 检查上游代理的 URL 和凭据,并确认本地端口没有被拦(CI 环境中很常见)。
OSError: Chromium downloadable not found at https://storage.googleapis.com/chromium-browser-snapshots/...
requests-html 依赖的 pyppeteer 无法下载它固定版本的 Chromium(requests-html #571)。 requests-html(最后发布于 2019 年)和 pyppeteer 都已无人维护;请迁移到 Playwright。
ImportError: lxml.html.clean module is now a separate project lxml_html_clean
lxml 5.2 拆分出了 requests-html 引用的模块(#569)。 pip install lxml_html_clean。

报错原文引自 GitHub issue(selenium-wire、SeleniumBase #2782、requests-html),并对照 selenium-wire 5.1.0 和 undetected-chromedriver 3.5.5 源码核实,核对日期 2026-12-01。

不用 selenium-wire、只用 undetected-chromedriver 也被拦?请看 undetected-chromedriver 2026 年失效 →

Cloudflare 为什么能识别 selenium-wire

selenium-wire(github.com/wkeeling/selenium-wire,MIT)曾是在 Selenium 中读取请求、使用带认证代理的便捷方案。它的原理是在本地运行一个基于 mitmproxy 的代理。README 写明它使用自己的根证书来解密 HTTPS 流量。浏览器与 selenium-wire 完成 TLS 握手,selenium-wire 再向网站建立第二条连接。

问题就出在第二条连接上。Cloudflare 读取的是直接连接它的那一方的 TLS 指纹,而这里是 selenium-wire 的 Python 网络栈,不是 Chrome。作者在多个 issue 中说明过:由于在后台使用 HTTPS 拦截,Selenium Wire 呈现的 TLS 指纹与浏览器不同(#215);在标题为“Using vanilla undetected chromedriver can get through CloudFlare, while selenium-wire UC cannot”的 #577 中,他还写道“目前遗憾地没有解决办法”。undetected_chromedriver 集成能修补浏览器,却改变不了一个浏览器根本没有参与的握手。

修复 1:让它在新版 Python 上能安装、能导入

该仓库已于 2024-01-03 归档,PyPI 上的最后一个版本是 2022 年 10 月的 5.1.0。之后有三项变化让它失效:blinker 1.8.0 移除了内置 mitmproxy 引用的 blinker._saferef;Python 3.12 的 venv 不再预装 setuptools;setuptools 82.0.0 移除了 selenium-wire 仍在引用的 pkg_resources。它的常用搭档 undetected-chromedriver 3.5.5 则引用了 Python 3.12 已移除的 distutils。SeleniumBase issue #2782 记录了 blinker 的修复方法。把三者都固定住:

pins.sh
# selenium-wire 5.1.0 (Oct 2022) was never updated for these changes:
#  - blinker 1.8 (Apr 2024) removed blinker._saferef, which selenium-wire's bundled mitmproxy imports
#  - Python 3.12 venvs no longer pre-install setuptools (pkg_resources, distutils)
#  - setuptools 82 (Feb 2026) removed pkg_resources
pip install "selenium-wire==5.1.0" "blinker==1.7.0" "setuptools<82"

# Quick check that the import chain loads
python -c "import seleniumwire.undetected_chromedriver as uc; print('ok')"

修复 2:保留代理,停止解密(disable_capture)

如果你保留 selenium-wire 只是为了使用带认证的上游代理,可以让它停止拦截。作者在 #215 中给出过这个建议:用 disable_capture 选项关闭请求捕获,同时也会关闭 HTTPS 解密。这样 Chrome 自己的握手会通过隧道直达网站。代价是 driver.requests 不再有数据,而且这并不能让 Chrome 本身变得不可检测,但它去掉了 selenium-wire 的指纹。请在目标网站上实测;已经没有维护者来修复回归问题了。

proxy_only.py
import seleniumwire.undetected_chromedriver as uc

PROXY = "http://user:pass@proxy.example.com:8080"

seleniumwire_options = {
    "proxy": {"http": PROXY, "https": PROXY, "no_proxy": "localhost,127.0.0.1"},
    # No request capture = no HTTPS decryption (maintainer, issue #215), so the
    # TLS handshake the site sees is Chrome's own instead of selenium-wire's.
    # Trade-off: driver.requests stays empty.
    "disable_capture": True,
}

driver = uc.Chrome(options=uc.ChromeOptions(), seleniumwire_options=seleniumwire_options)
try:
    driver.get("https://target-site.com/")
    print(driver.title)
finally:
    driver.quit()

修复 3:证书警告与代理连接错误

  • “Not Secure”或“Your connection is not private”:开启捕获时,每个页面都由 selenium-wire 的 CA 签名。这是拦截的工作方式,不是 bug。如果需要干净的页面,请在浏览器配置中安装它的根证书,或者使用 disable_capture。
  • net::ERR_PROXY_CONNECTION_FAILED:要么 Chrome 连不上 selenium-wire 的本地代理(CI 中端口被防火墙拦截,issue #719),要么 selenium-wire 连不上你的上游代理(协议、凭据或主机写错,#470 和 #425)。先用 curl 测试上游代理。
  • 为 localhost 设置 no_proxy,避免 Chrome 驱动自身的流量也走上游代理。
  • selenium-wire-2(0.2.1,2024 年 7 月,基于 mitmproxy 10.3 及以上)和 selenium-wire-lw(3.0.2,2026 年 1 月)等分支确实存在,并解决了部分安装问题。selenium-wire-2 仍通过 mitmproxy 拦截,所以上面的 TLS 问题依然存在;在依赖任何分支应对 Cloudflare 之前,先弄清它如何处理 TLS。

按你实际的用途替换 selenium-wire

大多数项目使用 selenium-wire 是为了下面两件事之一。现在每一件都有不需要在 Chrome 和网站之间插入解密代理的方案:

  • 带认证的代理:SeleniumBase 直接接受 proxy="USER:PASS@SERVER:PORT",并可与 UC Mode 以及其文档中作为 UC Mode 继任者的 CDP Mode 组合使用(SeleniumBase 4.59.2,2026-11-29)。如果仍被挑战,请看我们的 SeleniumBase UC Mode 指南。
  • 读取或修改请求:使用 Selenium 4 自带的网络功能。Chrome 的 DevTools 协议可以通过 driver.execute_cdp_cmd 调用,Selenium 4.32 还为 Python 加入了 BiDi 网络拦截与认证实现。具体 API 请查阅你所用版本的 Selenium BiDi 网络文档。
  • 不一定要用 Selenium?nodriver 和基于 Playwright 的工具都通过 CDP 与浏览器通信,中间没有拦截代理。
seleniumbase_proxy.py
# pip install -U seleniumbase
from seleniumbase import SB

URL = "https://target-site.com/"

# Authenticated proxy without a man-in-the-middle proxy in the path
with SB(uc=True, proxy="user:pass@proxy.example.com:8080", locale="en") as sb:
    sb.activate_cdp_mode(URL)
    sb.solve_captcha()          # only acts if a CAPTCHA is still on the page
    print(sb.get_title())

requests-html:同一时代,同样的问题

在较老的爬虫教程里,requests-html 经常和 selenium-wire 一起出现。它的最后一个版本是 2019 年 2 月的 0.10.0。它的渲染功能依赖 pyppeteer,而 pyppeteer 的 README 现在写着“This repo is unmaintained”,并推荐改用 playwright-python。如今大家遇到的两个报错是 Chromium 下载失败(“OSError: Chromium downloadable not found ...”,#571),以及 lxml 5.2 拆分出 lxml_html_clean(#569,pip install lxml_html_clean 即可修复)。即使能跑起来,它那个老旧的无头 Chromium 也很容易被 Cloudflare 识别。请把渲染迁移到 Playwright,或者干脆对 Cloudflare 页面不用浏览器(见下文)。

用 API 更简单的情况:为 Selenium 流程获取 Turnstile token

保留浏览器的常见原因之一,是登录或注册表单上有 Cloudflare Turnstile 组件。NSLSolver 在服务端求解 Turnstile,自身不使用浏览器:把站点密钥和页面 URL POST 到 https://api.nslsolver.com/solve,平均约 250 毫秒即可拿到 token,每 1,000 次 $0.80,只对成功的求解计费。提交前把它填入 cf-turnstile-response 字段(如果网站使用回调,再调用组件的回调函数)。根据 Cloudflare 文档,token 有效期 300 秒,且应只使用一次。

对于整页的“Just a moment...”挑战,Cloudflare Challenge 求解服务 返回 cf_clearance cookie 和 user_agent(每 1,000 次 $1.00,约 3.3 秒)。clearance 是为 iPhone Safari 浏览器签发的,所以请用匹配的 HTTP 客户端重放(见 cf_clearance 指南),而不是把它注入桌面版 Chrome。使用 Google、Gmail 或 Proton 注册可获免费试用额度。请只对你拥有或已获授权访问的网站进行自动化。

selenium_turnstile.py
import json

import requests
import seleniumwire.undetected_chromedriver as uc  # or plain selenium / SeleniumBase

URL = "https://target-site.com/login"

driver = uc.Chrome(options=uc.ChromeOptions(), seleniumwire_options={"disable_capture": True})
driver.get(URL)

site_key = driver.find_element("css selector", "[data-sitekey]").get_attribute("data-sitekey")

# One POST, about 250 ms on average, no second browser
token = requests.post(
    "https://api.nslsolver.com/solve",
    headers={"X-API-Key": "YOUR_API_KEY"},
    json={"type": "turnstile", "site_key": site_key, "url": URL},
    timeout=120,
).json()["token"]

# Put the token where the form expects it (valid 300 s, single use)
driver.execute_script("""
  let input = document.querySelector('[name="cf-turnstile-response"]');
  if (!input) {
    input = Object.assign(document.createElement('input'), {type: 'hidden', name: 'cf-turnstile-response'});
    document.querySelector('form')?.appendChild(input);
  }
  input.value = arguments[0];
""", token)

callback = driver.find_element("css selector", "[data-sitekey]").get_attribute("data-callback")
if callback:  # some sites only submit from the widget's callback
    driver.execute_script("window[arguments[0]](arguments[1])", callback, token)

driver.find_element("css selector", "button[type=submit]").click()
print(json.dumps({"title": driver.title}))
driver.quit()

完整 /solve API 参考

相关指南

常见问题

为什么 seleniumwire.undetected_chromedriver 会被 Cloudflare 检测到?

因为 selenium-wire 作为中间人代理解密 HTTPS,并自己向网站建立上游 TLS 连接。Cloudflare 看到的是这次握手,而不是 Chrome 的。作者在 issue #215、#566 和 #577 中确认过;undetected-chromedriver 改变不了浏览器已经不再参与的握手。

selenium-wire 还在维护吗?

没有。仓库已于 2024-01-03 归档,README 开头写着“Selenium Wire is no longer being maintained.”。PyPI 上的最后一个版本是 2022 年 10 月的 5.1.0。

如何修复“No module named 'blinker._saferef'”?

安装 blinker 1.7.0(pip install "blinker==1.7.0")。blinker 1.8 移除了 selenium-wire 内置 mitmproxy 所引用的模块。在 Python 3.12 上,通常还需要安装 "setuptools<82" 来提供 pkg_resources。

能否只用 selenium-wire 做代理认证而不被检测?

根据作者的说法(#215),设置 disable_capture 会关闭 HTTPS 解密,这样 Chrome 自己的握手会到达网站,上游代理仍然可用,只是失去了请求捕获。SeleniumBase 的 proxy="user:pass@host:port" 选项是一个仍在维护的替代方案。

应该用什么替代 selenium-wire?

带认证的代理用 SeleniumBase 的 UC 或 CDP Mode;查看请求用 Selenium 4 的 CDP 和 BiDi 网络功能;表单上的 Cloudflare 组件,则可以直接从求解 API 获取 Turnstile token,从根本上绕开这个问题。

NSLSolver 能和 Selenium 一起用吗?

可以。从页面读取 data-sitekey,与 URL 一起以 type turnstile POST 到 /solve,再把返回的 token 填入 cf-turnstile-response(或传给组件的回调)。耗时约 250 毫秒,每 1,000 次成功求解 $0.80。

表单上有 Turnstile?用 API 拿 token

流程继续交给 Selenium,由 NSLSolver 返回 Turnstile token:约 250 毫秒,每 1,000 次 $0.80,失败不计费。使用 Google、Gmail 或 Proton 注册即送免费试用额度,无需信用卡。