patchright 失效,通常只有四类原因:当前版本需要的浏览器没装好;用了无头模式;启动参数里加了它本来就要避免的指纹(自定义 UA、额外请求头、扩展);或者 Cloudflare 针对的是你的运行环境和 IP,而不是自动化协议。先把安装问题解决,再按 README 的最佳实践启动(真实 Chrome、有头、持久化上下文、不设 viewport、不注入任何东西),升级越过已知的回归版本,并用干净的 IP 测试。如果某个组件仍然循环,就从求解 API 拿 Turnstile token 再注入页面。
Patchright(Kaliiiiiiiiii-Vinyzu/patchright,Apache-2.0 许可证,约 5,300 星)是 Playwright 的打过补丁、可直接替换的版本。它去掉了 playwright-stealth 这类工具碰不到的协议层泄露;playwright-stealth 本身的问题请看我们的 [playwright-stealth 失效排查](/blog/playwright-stealth-not-working)。本文专门深入 patchright:它的具体报错、背后的 GitHub issue,以及维护者明确说不在范围内的部分。
快速答案:按 patchright 报错找修复方法
patchright 的大多数问题都对应下面某一条报错或现象。先找到你遇到的那条,再看下文对应章节里的代码。
- Executable doesn't exist at … Looks like Playwright was just installed or updated.
- 当前 patchright 版本需要的浏览器没有安装。 每次升级后运行 patchright install chromium(Python)或 npx patchright install chromium(Node)。Node 版报错里提示的仍是 npx playwright install,那是 Playwright 官方的命令行,不是 patchright 的。
- Chromium distribution 'chrome' is not found at …
- 传了 channel="chrome",但本机没有安装 Google Chrome。 运行 patchright install chrome。在 Linux arm64 上该脚本会输出 "ERROR: not supported on Linux Arm64"(patchright-python #121 仍未关闭),这种机器上请去掉 channel,改用自带的 Chromium。
- 有头能过,headless=True 就被拦或循环
- 无头 Chrome 可以被识别,patchright 不负责隐藏。 维护者的立场是:不自己编译 Chromium,无头模式就做不到完全不被检测;UA 里仍然带着 HeadlessChrome(issue #82、#227)。请用 headless=False,Linux 服务器上配合 Xvfb。
- 点击后 Turnstile 一直停在 "Verifying..." 循环
- 你的配置里有和真实浏览器矛盾的地方。 去掉自定义 user_agent、额外请求头和扩展。issue #73 里,写死的 Chrome 134 UA 就是循环的原因。按 README 的推荐方式启动,并换更干净的 IP。
- 本地正常,Docker 或 GitHub Actions 里失败
- 环境和网络指纹问题,patchright 不覆盖这一层。 issue #224 被标记为 #37 的重复,维护者在 #37 中写道,指纹识别“目前不在 patchright 的覆盖范围内”。先在普通桌面机器、住宅或 ISP IP 上测试,再判断是不是库的问题。
- 对 Turnstile 复选框 frame.locator().click() 没有反应
- 1.60.0 到 1.61.1 的已知回归(issue #203)。 维护者的答复是升级到 1.61.2 或更高版本。目前也就是 PyPI 上的 1.64.4 或 npm 上的 1.64.3。
- page.evaluate 读页面全局变量返回 undefined / console 事件从不触发
- 这两点都是设计如此。 evaluate 默认在隔离环境里执行:要读取 window 上的变量,请传 isolated_context=False(Node 里是 isolatedContext: false)。Console API 被整体禁用,用来堵住 Console.enable 泄露。
- ERR_TUNNEL_CONNECTION_FAILED,或走代理时 load 事件永远不触发
- 代理加初始化脚本、带认证代理的 bug,均已修复。 issue #166(1.57.0,代理加 add_init_script)和 issue #259(1.64.1,带认证代理,2026-11-02 以“升级到最新版”关闭)都已修复。升级后重新安装浏览器即可。
报错原文引自 patchright-core 1.64.3 安装包和文中引用的 issue;行为说明来自 patchright 各仓库的 README,均于 2026-12-01 核实。
patchright 的原理,以及它不补什么
patchright 由三个仓库组成:改写 Playwright 驱动的补丁器(patchright),以及 Python 包(patchright-python,约 1,600 星)和 Node.js 包(patchright-nodejs,约 800 星),都使用 Apache-2.0 许可证。只需改 import(from patchright.sync_api import sync_playwright,或 require('patchright')),其余 Playwright 代码保持不变。它只给基于 Chromium 的浏览器打补丁,不支持 Firefox 和 WebKit。
README 列出了它的补丁:通过在隔离的执行上下文里运行 JavaScript 来避免 Runtime.enable(它称之为“最大的补丁”);整体禁用 Console API 以堵住 Console.enable 泄露;修改默认启动参数(加上 --disable-blink-features=AutomationControlled,去掉 --enable-automation、--disable-extensions、--disable-component-update 等);修补一些通用泄露;并让普通定位器和 XPath 能进入 closed shadow root。初始化脚本是通过 Playwright 路由注入的,README 承认这可能被时序攻击发现。
它刻意不做的事同样重要。在 issue #37 中,维护者写道 patchright“目前不打算覆盖指纹识别”;对于无头模式,主维护者的立场(issue #82、#113)是:不自己编译 Chromium 就无法做到不被检测。README 把 Cloudflare 列为“配置正确时”可以通过的防护之一,而大多数失败恰恰来自偏离了这套配置。
patchright 为什么会被拦或出错
- 无头模式:headless=True 时 UA 里仍有 "HeadlessChrome",而 patchright 不会改写它,用维护者的话说,修改 UA“会引入其他泄露”。
- 额外加上的指纹:写死的 UA、自定义请求头、扩展、在同一个配置文件里轮换代理,都是 #37 中维护者首先指出的问题。#73 中 Turnstile 循环的原因就是 UA 里的 Chrome 版本不对。
- 运行环境:Docker 镜像、CI 运行器和 Linux aarch64 与普通桌面差别明显,维护者称 aarch64“对反爬系统来说可能就是一个危险信号”。机房 IP 无论浏览器多干净都会拉低评分。
- 版本变动:每次 Playwright 发布后都会自动部署新版本,README 提醒由 Playwright 改动引起的 bug“可能需要几天”才能修好。实际例子:1.57.x 打包进了原版 Playwright 驱动(patchright-python #95),1.60.0 中 Turnstile 点击失效(#203),1.64.1 中带认证代理会卡住(#259)。
- 浏览器文件不匹配:每个基于 Playwright 的版本都需要特定的浏览器构建。patchright 比上游慢一个版本(PyPI 上是 1.64.4,Playwright 已到 1.65),所以用官方 Playwright 命令行安装的浏览器,不一定是 patchright 要找的那个。
修复 1:为当前版本安装对应的浏览器
"Executable doesn't exist at …" 和 "Chromium distribution 'chrome' is not found" 是安装问题,不是被检测。每次升级后用 patchright 命令行重新安装浏览器,并锁定包版本,避免项目进行到一半时被新的自动发布打乱。注意 patchright-python 的 GitHub releases 页面停在 v1.62.0(2026 年 8 月 17 日),当前版本请以 PyPI 为准。
在 Linux arm64 上暂时无法通过命令行安装 Google Chrome(patchright-python #121 仍未关闭),这类机器请使用自带的 Chromium。打包成可执行文件也是已知短板:patchright-python #45(PyInstaller 和 Nuitka 不会打包 Chromium)仍处于打开状态。
# Python 3.10+: the package, then the browsers THIS patchright version expects
pip install -U patchright
patchright install chromium # bundled Chromium build
patchright install chrome # Google Chrome stable, for channel="chrome"
# Node.js 20+
npm i patchright
npx patchright install chromium
npx patchright install chrome # use the patchright CLI, not "npx playwright install"
# Pin the version so an automatic upstream release cannot change behaviour between deploys
pip install "patchright==1.64.4" # PyPI, 2026-11-02
npm i patchright@1.64.3 # npm, 2026-11-02
# Linux server without a display: run the headed browser inside Xvfb
xvfb-run -a python app.py修复 2:按文档推荐的方式启动
README 推荐的配置是 launch_persistent_context,配合 channel="chrome"、headless=False 和 no_viewport=True,不设自定义请求头和 UA。持久化配置文件能在多次运行之间保留 cf_clearance 等 cookie。在服务器上,请把有头浏览器放进 Xvfb 里跑,维护者推荐用它替代无头模式。
如果 Chrome 顶部出现 "You are using an unsupported command-line flag: --disable-blink-features=AutomationControlled" 提示条,这只是外观问题:维护者在 #238 中表示网站检测不到它,后来 Chrome 一侧也已解决。不要用 ignore_default_args 去掉这个参数,否则 navigator.webdriver 会重新变成 true。
from patchright.sync_api import sync_playwright
TARGET = "https://example.com/"
with sync_playwright() as p:
# The README's "best practice": real Chrome, headed, persistent profile,
# no viewport emulation and no fingerprint injection of any kind.
context = p.chromium.launch_persistent_context(
user_data_dir="./patchright-profile",
channel="chrome", # needs: patchright install chrome
headless=False, # headless is detectable; use Xvfb on servers
no_viewport=True, # keep the real window size
# do NOT set user_agent, extra_http_headers or load extensions
# proxy={"server": "http://host:port", "username": "u", "password": "p"},
)
page = context.pages[0] if context.pages else context.new_page()
page.goto(TARGET)
print(page.title())
context.close()
# Node.js equivalent:
# chromium.launchPersistentContext("./patchright-profile",
# { channel: "chrome", headless: false, viewport: null })修复 3:Turnstile 与 "Just a moment..." 挑战
在干净的有头浏览器里,非交互式挑战往往会自动通过,所以先等待 cf_clearance 出现或 cf-turnstile-response 被填上,再考虑点击。复选框最多点一次,并且要在 challenges.cloudflare.com 的 frame 里点。如果在 1.60.0 到 1.61.1 上点击能执行却毫无反应,那就是回归问题 #203,升级即可。组件里的 shadow DOM 访问在 1.56.0 之后也出现过回归(#173,已于 2026 年 3 月修复)。
一些旧回答会推荐用 CDP-Patches 做系统级点击。该仓库已于 2025 年 9 月 28 日归档,README 现在写明底层的 Chromium 输入泄露已经修复,“除了 Select 元素外,没有理由再使用这个包”。
from patchright.sync_api import sync_playwright
def turnstile_frame(page):
return next((f for f in page.frames if "challenges.cloudflare.com" in f.url), None)
def wait_for_cloudflare(page, context, timeout_s=30):
"""Wait for cf_clearance or a Turnstile token; click the checkbox at most once."""
clicked = False
for second in range(timeout_s):
if any(c["name"] == "cf_clearance" for c in context.cookies()):
return "cleared"
# The DOM is shared between worlds, so the default isolated context can read it
token = page.evaluate(
"() => document.querySelector('[name=\"cf-turnstile-response\"]')?.value || ''"
)
if token:
return "turnstile-token"
frame = turnstile_frame(page)
if frame and not clicked and second >= 5:
try:
# patchright locators reach into the widget's closed shadow root
frame.get_by_role("checkbox").click(timeout=3000)
clicked = True
except Exception:
pass
page.wait_for_timeout(1000)
return "timeout"
with sync_playwright() as p:
context = p.chromium.launch_persistent_context(
"./patchright-profile", channel="chrome", headless=False, no_viewport=True
)
page = context.pages[0] if context.pages else context.new_page()
page.goto("https://example.com/")
print(wait_for_cloudflare(page, context))
# Page globals (window.turnstile, site callbacks) live in the main world:
print(page.evaluate("() => typeof window.turnstile", isolated_context=False))
context.close()修复 4:evaluate、初始化脚本、console 与代理
- 主环境里的全局变量:evaluate、evaluate_handle 和 evaluate_all 都有 isolated_context 参数(默认 True)。要读取 window.turnstile 或调用站点的回调,请传 isolated_context=False;只读 DOM 时保持默认即可。
- console:page.on("console") 不会触发。README 建议确实需要输出时改用页面内的日志工具,同时提醒这类工具也可能被检测。
- 初始化脚本:2025 年初的 issue #41 显示,Cloudflare 能通过可见的 script 标签和注入请求的 TLS 指纹识别 add_init_script。两者都在 2025 年 3 月修复了,但如果某个站点只在使用初始化脚本时失败,先去掉它测试。
- 代理:通过 proxy 参数传入账号密码,每个配置文件固定一个出口 IP,并保持最新版本;#166 和 #259 都是后续版本修复的代理 bug。
- 不要再叠加 playwright-stealth。patchright 的最佳实践就是“不注入指纹”。如果你在比较不同方案,可以看 rebrowser-patches 失效排查 和我们的 开源 Turnstile 与 Cloudflare 求解工具对比。
什么时候该改用 API
当你需要真实的浏览器会话(登录、点击、多步流程)时,patchright 是合适的工具。但如果你在 Xvfb 下跑几十个有头 Chrome 只是为了给每个表单拿一个 token,做完上面的修复后同一个组件仍在循环,或者每次自动发布都要重新做回归测试,那就说明你在错误的层面上较劲。
这时让 patchright 继续负责页面导航,只把 Turnstile 这一步交给求解服务。请只对你拥有或已获授权访问的网站做自动化,并遵守对方的条款和频率限制。
NSLSolver 示例:为 patchright 页面获取 Turnstile token
Cloudflare Turnstile 求解 API 接收组件的 site_key 和页面 url(组件设置了 action、cdata 时才需要传),平均约 250 毫秒返回 token。求解在服务端基于请求完成,我们这边也不运行浏览器。由于站点的回调函数在主环境里,请在主环境中把 token 作为 cf-turnstile-response 注入。每 1,000 次 0.80 美元,失败不收费(503 不计费,可以安全重试),使用 Google、Gmail 或 Proton 注册即送免费试用额度;详见 价格。
如果拦你的是整页的 "Just a moment..." 挑战,Cloudflare Challenge 求解 API 会返回 cf_clearance 以及需要通过你传入的同一代理复用的 user_agent(约 3.3 秒,每 1,000 次 1.00 美元)。这个 clearance 是为 iPhone Safari 浏览器签发的,所以应在普通 HTTP 客户端里复用,而不是塞回桌面版 Chrome;原因见 cf_clearance 指南。NSLSolver 不支持 reCAPTCHA v2、hCaptcha、图片验证码和 DataDome。
import requests
from patchright.sync_api import sync_playwright
API_KEY = "nsl_YOUR_API_KEY"
def solve_turnstile(site_key, url, action=None, cdata=None):
payload = {"type": "turnstile", "site_key": site_key, "url": url}
if action:
payload["action"] = action # only if the widget sets data-action
if cdata:
payload["cdata"] = cdata # only if the widget sets data-cdata
r = requests.post(
"https://api.nslsolver.com/solve",
headers={"X-API-Key": API_KEY},
json=payload,
timeout=120,
)
if r.status_code == 503: # not completed: not billed, safe to retry
raise RuntimeError("retry")
r.raise_for_status()
return r.json()["token"]
INJECT = """(token) => {
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 = token;
const cb = document.querySelector('[data-callback]')?.getAttribute('data-callback');
if (cb && "undefined"[cb] === 'function') window[cb](token);
}"""
with sync_playwright() as p:
context = p.chromium.launch_persistent_context(
"./patchright-profile", channel="chrome", headless=False, no_viewport=True
)
page = context.pages[0] if context.pages else context.new_page()
page.goto("https://example.com/login")
widget = page.locator("[data-sitekey]").first
token = solve_turnstile(
widget.get_attribute("data-sitekey"),
page.url,
widget.get_attribute("data-action"),
widget.get_attribute("data-cdata"),
) # ~250 ms on average; a token is single-use and valid for 300 s
# isolated_context=False: the site's callback lives in the main world
page.evaluate(INJECT, token, isolated_context=False)
page.click("button[type=submit]")
context.close()相关指南
- Playwright Stealth 失效排查:stealth_sync 导入错误、Runtime.enable 泄露,以及 patchright 与 rebrowser 的对比。
- rebrowser-patches 失效排查:另一种给 Playwright 和 Puppeteer 打补丁的方案。
- cf-turnstile-response 无效:注入的 token 为什么在提交时被拒。
- cf_clearance cookie 详解:它为什么和 IP、User-Agent 绑定。
- Cloudflare Challenge 求解 API:一次调用拿到 cf_clearance 和 user_agent。
参考来源(2026-12-01 核实)
- GitHub:Kaliiiiiiiiii-Vinyzu/patchright(补丁列表、初始化脚本说明、已知 bug)
- GitHub:patchright-python README(安装、最佳实践、isolated_context)
- GitHub:patchright-nodejs README(npx 安装、isolatedContext)
- PyPI:patchright(1.64.4,2026-11-02,Python 3.10+)
- npm:patchright(1.64.3,2026-11-02,Node 20+)
- patchright-python releases(最新标签 v1.62.0,自动部署)
- Issue #37:托管挑战失败,指纹识别不在范围内
- Issue #82:无头模式 UA 中的 HeadlessChrome
- Issue #73:自定义 UA 导致 Cloudflare 验证循环
- Issue #203:1.60.0 中 Turnstile 点击回归
- Issue #224:本地正常,Docker / GitHub Actions 中失败
- Issue #41:add_init_script 被 Cloudflare 检测(已修复)
- Issue #259:1.64.1 带认证代理卡住(已修复)
- patchright-python issue #121:Linux arm64 无法安装 chrome
- CDP-Patches README(已于 2025-09-28 归档)