OpenClaw Browser:让Agent成为你的浏览器替身

📅 2026-03-19 ⏱️ 阅读时间: 9分钟 🏷️ Browser | 浏览器自动化 | Playwright

凌晨3点17分,Agent正在浏览器里帮你抢票。它不会困,不会手抖,更不会因为网速慢而崩溃。这就是浏览器自动化的魅力——让AI替你上网。

什么是 Browser 自动化?

OpenClaw内置基于Playwright的浏览器控制能力:

Browser 工具基础

启动浏览器

{
  "action": "start",
  "profile": "chrome"  // 使用Chrome扩展
}

打开页面

{
  "action": "open",
  "url": "https://example.com"
}

页面快照

{
  "action": "snapshot",
  "refs": "aria"  // 获取可交互元素引用
}

常用操作示例

点击元素

{
  "action": "act",
  "request": {
    "kind": "click",
    "ref": "e12"  // 从snapshot获取的元素引用
  }
}

输入文本

{
  "action": "act",
  "request": {
    "kind": "type",
    "ref": "e5",
    "text": "搜索内容",
    "submit": true  // 输入后按回车
  }
}

截图

{
  "action": "screenshot",
  "fullPage": true,
  "type": "png"
}

实战案例:自动填表

// 1. 打开表单页面
browser action=open url="https://example.com/form"

// 2. 获取页面快照
browser action=snapshot refs="aria"

// 3. 填写姓名
browser action=act request={
  "kind": "type",
  "ref": "e3",
  "text": "张三"
}

// 4. 填写邮箱
browser action=act request={
  "kind": "type", 
  "ref": "e4",
  "text": "zhangsan@example.com"
}

// 5. 选择下拉选项
browser action=act request={
  "kind": "select",
  "ref": "e5",
  "values": ["option1"]
}

// 6. 提交表单
browser action=act request={
  "kind": "click",
  "ref": "e10"
}

数据抓取

使用evaluate执行JavaScript提取数据:

{
  "action": "act",
  "request": {
    "kind": "evaluate",
    "fn": "() => {
      const items = document.querySelectorAll('.item');
      return Array.from(items).map(item => ({
        title: item.querySelector('h3').textContent,
        price: item.querySelector('.price').textContent
      }));
    }"
  }
}

Chrome扩展模式

OpenClaw支持接管已有的Chrome标签页:

  1. 安装OpenClaw Browser Relay扩展
  2. 在目标标签页点击扩展图标
  3. 使用 profile: "chrome" 启动
{
  "action": "start",
  "profile": "chrome"
}

多标签页管理

// 查看所有标签页
browser action=tabs

// 切换到指定标签页
browser action=focus targetId="tab-123"

// 关闭标签页
browser action=close targetId="tab-123"

处理弹窗和对话框

// 接受alert
browser action=dialog accept=true

// 输入prompt内容
browser action=dialog accept=true promptText="输入内容"

等待策略

// 等待元素出现
browser action=act request={
  "kind": "wait",
  "selector": ".loading",
  "timeoutMs": 5000
}

// 等待页面加载完成
browser action=act request={
  "kind": "wait",
  "loadState": "networkidle"
}

最佳实践

安全注意事项

相关链接

🌐 想学习更多自动化玩法?加入 Discord社区 与高手交流!