How to Add a CAPTCHA Solver to Patchright Automation
说明与用途
Patchright 的隔离执行上下文阻止了 CAPTCHA 检测。通过适配器将 SDK 调用转发到页面主上下文,可使用 CapSolver 检测并解决 reCAPTCHA。
问题
Patchright browser automation does not provide a CAPTCHA response because its default isolated evaluation context hides page‑owned globals needed for reCAPTCHA detection.
结果
The adapter enables reCAPTCHA detection in Patchright by routing SDK evaluation to the main page context. Detection succeeds on the public test widget; solving requires a valid CapSolver API key and is not demonstrated in the example.
工作流
- Install Patchright 1.64.1 and capsovler-core 0.1.1 in a Python 3.12 virtual environment.
- Create a local test page with Google’s public reCAPTCHA test widget.
- Define a MainWorldPage wrapper that forwards evaluate() calls to the page with isolated_context=False.
- Launch Patchright Chromium, open the test page, and wait for the reCAPTCHA selector.
- Use CapSolver’s create_capsolver() to instantiate a client (no key needed for detection).
- Call the SDK’s detect() method via the wrapper to retrieve reCAPTCHA parameters.
- (Optional) With a valid API key, call solve() to obtain a token and fill it back into the form.
- Verify that the token is returned and the form can be submitted.
证据
Patchright controls Chromium, but browser automation alone does not provide a CAPTCHA response.
Patchright’s evaluate() option isolated_context defaults to True, so an ordinary evaluation may not see page‑owned globals even while the widget is visible in the DOM.
The adapter below forwards the SDK’s evaluation calls to the page’s main environment.
The executed example used Python 3.12, Patchright 1.64.1, and capsovler-core 0.1.1.
- 记录日期: 2026-10-22T09:26:06.225Z