OpenClaw Browser:让Agent成为你的浏览器替身
凌晨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标签页:
- 安装OpenClaw Browser Relay扩展
- 在目标标签页点击扩展图标
- 使用
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"
}
最佳实践
- 使用aria引用:比selector更稳定
- 添加等待:确保元素加载完成
- 处理异常:网页可能随时变化
- 合理超时:避免无限等待
- 截图留证:关键步骤截图备份
安全注意事项
- 敏感信息:不要在日志中暴露密码
- 权限控制:限制可访问的域名
- 验证码:需要人工介入处理
相关链接
🌐 想学习更多自动化玩法?加入 Discord社区 与高手交流!