如果你用 Playwright 做网页自动化,大概遇到过脚本被网站拦截、弹出验证码甚至直接拒绝访问的情况。这不是代码的问题,而是 Playwright 自身携带了一些容易被反机器人系统检测到的“特征”。
Patchright 正是为解决这个问题而生——它是 Playwright 的一个补丁版本,专注于修复那些导致自动化被识别的底层信号。最关键的是,它是 即插即用 的:你只需把 from playwright 改成 from patchright,其余代码完全不变。目前支持 Python、Node.js 和 .NET。

为什么 Playwright 会被检测?
反机器人系统(如 Cloudflare、Akamai、Datadome)主要从两方面识别自动化:
浏览器指纹:WebGL、字体、屏幕分辨率等。
驱动层信号:Playwright 在通过 CDP(Chrome DevTools Protocol)控制浏览器时,会发送一些固定的命令,比如 Runtime.enable 和 Console.enable。这些命令可以被网站检测到。
另外,Playwright 默认会携带 --enable-automation 启动参数,这会让 navigator.webdriver 返回 true——几乎等于主动告诉网站“我是机器人”。
Patchright 的核心补丁
Patchright 针对上述问题做了三方面修补:
移除 Runtime.enable:改用隔离的执行上下文运行 JS,避免 CDP 命令被捕获。
禁用 Console.enable:彻底消除控制台信号(代价是 console.log() 无法使用)。
调整启动参数:移除 --enable-automation、--disable-popup-blocking 等容易被关联的开关,同时添加 --disable-blink-features=AutomationControlled,使 navigator.webdriver 返回 false 或 undefined。
此外,Patchright 还能自动穿透 Closed Shadow Roots,方便操作现代 Web Components。
与 Playwright 的简要对比
核心定位
Playwright:通用浏览器自动化/测试框架
Patchright:专注于反检测的补丁版本
API 兼容性
两者完全兼容,Patchright 只需将 from playwright 改为 from patchright,其余代码无需改动。
浏览器支持
Playwright:支持 Chromium、Firefox、WebKit
Patchright:仅支持 Chromium 系(官方推荐使用 Google Chrome)
自动化特征暴露
Playwright:navigator.webdriver 返回 true,携带 --enable-automation 等参数
Patchright:移除这些参数,navigator.webdriver 返回 false 或 undefined
控制台调试
Playwright:完整支持 console.log()
Patchright:为隐匿而禁用控制台(调试需改用 Python 的 print 或日志)
最佳适用场景
Playwright:跨浏览器测试、常规自动化、依赖调试的项目
Patchright:需要绕过 Cloudflare 等高级反爬、做大规模抓取或 AI Agent 的场景
隐匿效果与局限
Patchright 宣称在正确配置下可绕过 Cloudflare、Akamai、Datadome、Fingerprint.com 等主流防护,并通过了 CreepJS、Sannysoft 等指纹检测工具。
但它也有明显局限:
仅支持 Chromium(Firefox/WebKit 用户请绕道)。
控制台功能失效,调试依赖需要改用 Python 的 print 或日志。
作为分支项目,更新和维护节奏可能不及官方版本。
最佳实践配置
官方推荐使用以下方式启动,以获得最佳隐匿效果:
from patchright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch_persistent_context(
user_data_dir="...",
channel="chrome", # 使用真实 Chrome,而非 Chromium
headless=False, # 有头模式更不易被检测
no_viewport=True, # 不固定视口大小
)
通过 patchright install chrome 安装 Chrome 即可。
什么时候选 Patchright?
需要绕过高级反爬(如 Cloudflare) → Patchright。
做大规模抓取或 AI Agent → Patchright。
需要跨浏览器测试或依赖控制台调试 → 继续用 Playwright。
想把现有项目快速转为隐匿模式 → Patchright(改一行 import)。
一句话:Patchright 用极低的迁移成本,换来了显著的反检测收益。如果你是“隐匿优先”的玩家,它值得一试。 但若你更看重全面性和官方维护,Playwright 仍是更稳妥的基石。
转载请注明:思享SEO博客 » Patchright:web自动化反检测增强方案