某些浏览器自动化工作流需要人工干预。登录页面可能需要多因素身份验证,表单可能需要你不想传递给自动化脚本的敏感凭据,或者任务可能过于复杂而无法完全自动化。Human in the Loop 让人类通过 Live View 进入实时浏览器会话,处理自动化无法完成的部分,然后将控制权交还给脚本。
Human in the Loop 适用于任何 Browser Session,并使用 Live View 让人类访问:
- 自动化脚本导航到需要人工输入的页面。
- 脚本从会话的 target 列表中获取 Live View URL,并与人工操作员共享(例如通过 Slack、电子邮件发送或在用户界面中显示)。
- 人工操作员打开 Live View URL 并完成所需操作(登录、解决 CAPTCHA、输入敏感数据等)。
- 自动化脚本检测到人工已完成(例如等待导航事件或轮询页面元素),然后继续执行。
更结构化的交接流程(智能体可以发出需要帮助的信号并通知人工)即将推出。
此示例使用通过 CDP 端点连接到 Browser Run 的 Puppeteer。脚本导航到登录页面,共享 Live View URL 供人工输入凭据,登录完成后继续自动化。
import puppeteer from "puppeteer-core";
const ACCOUNT_ID = "<your-account-id>";
const API_TOKEN = "<your-api-token>";
// Create a browser session via CDP
const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/browser-rendering/devtools/browser?keep_alive=600000&targets=true`,
{
method: "POST",
headers: { Authorization: `Bearer ${API_TOKEN}` },
},
);
const { webSocketDebuggerUrl, targets } = await response.json();
const liveUrl = targets[0].devtoolsFrontendUrl;
// Connect Puppeteer to the session
const browser = await puppeteer.connect({
browserWSEndpoint: webSocketDebuggerUrl,
headers: { Authorization: `Bearer ${API_TOKEN}` },
});
const page = await browser.newPage();
await page.goto("https://example.com/login");
// Share the Live View URL with the human operator (for example, send it via Slack, email, or display it in a UI)
console.log(`Human input needed. Open this URL: ${liveUrl}`);
// Wait for the human to complete login (5 minute timeout — the script will continue after this period)
await page.waitForNavigation({ waitUntil: "networkidle0", timeout: 300000 });
// Login complete, continue automation
const cookies = await page.cookies();
console.log("Login complete. Continuing automation...");
await page.goto("https://example.com/dashboard");
const content = await page.content();
browser.disconnect();Live View URL 自生成起五分钟内有效。如果 URL 在人工操作员打开之前过期,请再次列出 target 以获取新的 URL。
- 身份验证流程:具有 MFA、SSO 或 CAPTCHA 的登录页面,无法以编程方式绕过
- 敏感数据输入:需要凭据或个人信息的表单,你不想传递给自动化脚本
- 复杂交互:过于困难或不值得完全自动化的一次性任务,例如配置仪表板或批准工作流
- 验证步骤:在脚本继续之前确认订单、审查生成的内容或批准操作