# 动态 CAPTCHA 解决

默认情况下，无头浏览器会在页面加载时自动检测并立即解决 CAPTCHA。不过，有些网站会在后续阶段显示 CAPTCHA，例如在提交表单时或在特定用户交互后弹出的窗口中。

无头浏览器允许您在会话中的任何时刻手动触发 CAPTCHA 检测和解决。要手动触发 CAPTCHA 解决，请执行以下代码，向 window 对象发送消息：

```
window.postMessage({action: 'solve_captcha', type: '<captcha type>'}, '*')
```

支持的 CAPTCHA 类型包括：

* `hcaptcha`
* `recaptcha`
* `turnstile` （Cloudflare CAPTCHA）

使用示例（Playwright / Puppeteer JavaScript）：

```
// ...
await page.click('#form-submit');
// 现在我们知道应该显示 CAPTCHA 了
await page.evaluate(() => {
  window.postMessage({action: 'solve_captcha', type: 'recaptcha'}, '*')
});
// 像常规情况一样关注 `oxylabs-captcha-start` 和 `oxylabs-captcha-end` 事件
// ...
```

`Turnstile` CAPTCHA 需要采用不同的方法，因为必须在它出现在屏幕上之前进行拦截。为此，请先启动 CAPTCHA 检测，然后执行会触发 CAPTCHA 的操作：

```
// 我们知道下一步操作可能会触发 Turnstile CAPTCHA
await page.evaluate(() => {
  window.postMessage({action: 'solve_captcha', type: 'turnstile'}, '*')
});
await page.click('#form-submit');
// 像常规情况一样关注 `oxylabs-captcha-start` 和 `oxylabs-captcha-end` 事件
// ...
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.oxylabs.io/products/cn/headless-browser/features/dynamic-captcha-solving.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
