Agent Browser - Vercel Labs 的 AI Agent 浏览器自动化神器
基本信息
- GitHub: vercel-labs/agent-browser
- ⭐ 星标: 18,345(两个月不到就冲到这个数字!)
- 语言: Rust(核心CLI)+ Node.js(Daemon)
- 开发者: Vercel Labs
- 许可证: MIT
简介
Agent Browser 是一个专门为 AI Agent 设计的无头浏览器自动化 CLI 工具。它使用 Rust 编写的原生二进制文件提供毫秒级性能,同时支持 Playwright 协议来管理浏览器实例。
与传统浏览器自动化工具不同,Agent Browser 从设计之初就考虑了 LLM(大语言模型)的需求和交互方式,提供了对 AI 友好的工作流。
核心特性
1. 极致性能
- Rust 原生二进制:用 Rust 编写的核心 CLI,提供亚毫秒级解析延迟
- 可选纯 Rust Daemon:完全绕过 Node.js,直接通过 Chrome DevTools Protocol (CDP) 通信
- 快速启动:浏览器实例通过后台 daemon 持久化,后续命令瞬间响应
2. AI 友好的工作流
Agent Browser 的核心工作流是为 LLM 优化的:
bash
# 1. 打开网页
agent-browser open https://example.com
# 2. 获取快照(带 AI 友好的 refs)
agent-browser snapshot
# 输出示例:
# - heading "欢迎" [ref=e1] [level=1]
# - button "提交" [ref=e2]
# - textbox "邮箱" [ref=e3]
# 3. 使用 refs 交互(无需重新查询 DOM)
agent-browser click @e2
agent-browser fill @e3 "[email protected]"Refs 机制的优势:
- 确定性:ref 直接指向快照中的特定元素,不会因为页面变化而失效
- 快速:无需重新查询 DOM,直接通过引用访问
- AI 友好:snapshot 返回结构化的无障碍树,LLM 可以直接理解
3. 智能查找功能
Agent Browser 提供了丰富的语义查找方式,让 AI 更容易定位元素:
bash
# 通过 ARIA 角色查找
agent-browser find role button click --name "提交"
# 通过文本内容查找
agent-browser find text "登录" click
# 通过 label 查找
agent-browser find label "邮箱" fill "[email protected]"
# 通过 placeholder 查找
agent-browser find placeholder "请输入密码" fill "password123"
# 通过 alt 文本查找
agent-browser find alt "logo" screenshot
# 通过 data-testid 查找
agent-browser find testid submit-button click4. 多会话支持
Agent Browser 支持同时运行多个隔离的浏览器会话:
bash
# 默认会话
agent-browser open site-a.com
# 不同的会话(完全隔离)
agent-browser --session agent1 open site-b.com
agent-browser --session agent2 open site-c.com
# 查看所有活跃会话
agent-browser session list每个会话都有:
- 独立的浏览器实例
- 独立的 Cookies 和 localStorage
- 独立的导航历史
- 独立的认证状态
5. 会话持久化
使用 --profile 或 --session-name 保存认证状态,重启浏览器后可以恢复:
bash
# 持久化 profile
agent-browser --profile ~/.myapp-profile open myapp.com
# 登录一次...
# 以后可以复用已登录的会话
agent-browser --profile ~/.myapp-profile open myapp.com/dashboard
# 自动保存 session
export AGENT_BROWSER_SESSION_NAME=twitter
agent-browser open twitter.com
# 登录后,状态自动保存,下次启动直接恢复6. 安全功能(全部可选)
Agent Browser 默认关闭所有敏感功能,必须显式启用:
认证保险箱
bash
# 存储凭证(本地加密存储,AI 永远看不到密码)
agent-browser auth save github --url https://github.com/login --username user --password-stdin
# 使用凭证登录
agent-browser auth login github
# 生成的加密密钥存储在 ~/.agent-browser/.encryption-key域名白名单
bash
# 限制只能访问可信域名(阻止爬虫行为)
agent-browser --allowed-domains "example.com,*.example.com" open site.com
# 子资源请求(scripts, images, WebSocket)也会被阻止
# 需要包含 CDN 域名:--allowed-domains "example.com,*.cdn.example.com"动作策略
bash
# 用 JSON 策略文件控制敏感操作
agent-browser --action-policy ./policy.json open site.com动作确认
bash
# 特定类别操作需要确认
export AGENT_BROWSER_CONFIRM_ACTIONS="eval,download"
agent-browser eval "window.location = 'https://malicious.com'" # 需要确认内容边界标记
bash
# 用分隔符包装输出,LLM 可以区分工具输出和恶意内容
agent-browser --content-boundaries eval "document.body.innerHTML"
# 输出格式:
# >>>CONTENT_START
# 页面内容
# >>>CONTENT_END输出长度限制
bash
# 防止 LLM 上下文溢出
export AGENT_BROWSER_MAX_OUTPUT=50000安装和使用
全局安装(推荐)
bash
npm install -g agent-browser
agent-browser install # 下载 Chromium临时使用(无需安装)
bash
npx agent-browser install # 首次运行需要
npx agent-browser open https://example.comHomebrew 安装
bash
brew install agent-browser
agent-browser install从源码编译
bash
git clone https://github.com/vercel-labs/agent-browser
cd agent-browser
pnpm install
pnpm build
pnpm build:native # 需要 Rust
pnpm link --globalLinux 系统依赖
bash
agent-browser install --with-deps
# 或手动安装:
npx playwright install-deps chromium常用命令
导航和交互
bash
# 打开网页
agent-browser open https://example.com
# 点击元素
agent-browser click "#submit"
# 双击
agent-browser dblclick "#button"
# 填充表单
agent-browser fill "#email" "[email protected]"
# 输入文本
agent-browser type "#textarea" "Hello World"
# 获取文本
agent-browser get text "#main"
# 等待元素
agent-browser wait "#button"
# 返回/前进
agent-browser back
agent-browser forward截图和快照
bash
# 截图(全页)
agent-browser screenshot --full page.png
# 带标注的截图(元素编号)
agent-browser screenshot --annotate
# 输出:
# [1] @e1 button "提交"
# [2] @e2 link "首页"
# [3] @e3 textbox "邮箱"
# 获取无障碍树(AI 友好)
agent-browser snapshot
# 带过滤器
agent-browser snapshot -i # 只显示交互元素
agent-browser snapshot -C # 包含 cursor-interactive 元素
agent-browser snapshot -c # 紧凑模式
agent-browser snapshot -d 3 # 限制深度
agent-browser snapshot -s "#main" # 限制到元素页面信息
bash
# 获取标题
agent-browser get title
# 获取 URL
agent-browser get url
# 获取 HTML
agent-browser get html "#container"
# 获取输入值
agent-browser get value "#email"
# 获取属性
agent-browser get attr "#image" "src"
# 检查可见性
agent-browser is visible "#button"
agent-browser is enabled "#input"Cookie 和存储
bash
# 获取所有 cookies
agent-browser cookies
# 设置 cookie
agent-browser cookies set session_id "abc123"
# 清除 cookies
agent-browser cookies clear
# localStorage
agent-browser storage local
agent-browser storage local set key "value"
agent-browser storage local clear
# sessionStorage
agent-browser storage session网络拦截
bash
# 拦截请求(阻止)
agent-browser network route https://api.example.com --abort
# Mock 响应
agent-browser network route https://api.example.com --body '{"success": true}'
# 移除路由
agent-browser network unroute https://api.example.com
# 查看所有请求
agent-browser network requests与 AI 工具集成
Claude Code
bash
# 添加为 skill
npx skills add vercel-labs/agent-browser
# 安装后,Claude Code 可以直接使用Cursor / Windsurf / GitHub Copilot
直接在 MCP 配置中添加:
json
{
"mcpServers": {
"agent-browser": {
"command": "npx",
"args": [
"@vercel-labs/agent-browser@latest"
]
}
}
}高级功能
设备模拟
bash
# 模拟移动设备
agent-browser set device "iPhone 14"
agent-browser set device "iPad Pro"
# 自定义视口
agent-browser set viewport 375 667代理设置
bash
# 设置代理
agent-browser --proxy http://proxy.example.com:8080 open site.com
# 带认证
agent-browser --proxy http://user:[email protected]:8080 open site.com
# 代理绕过
agent-browser --proxy http://proxy.example.com:8080 --proxy-bypass "internal.com" open site.comiOS 模拟器(macOS)
bash
# 控制 Safari on iOS
agent-browser -p ios --device "iPhone 16 Pro" open https://example.com
# 手势操作
agent-browser -p ios swipe up
agent-browser -p ios swipe down 500
# 点击
agent-browser -p ios tap @e1Chrome DevTools Protocol
bash
# 启动 Chrome with remote debugging
google-chrome --remote-debugging-port=9222
# 连接到现有 Chrome
agent-browser --cdp 9222 snapshot
# WebSocket 连接
agent-browser --cdp "wss://browser-service.com/cdp?token=..."视图流(远程预览)
bash
# 启动 WebSocket 服务器
export AGENT_BROWSER_STREAM_PORT=9223
agent-browser open https://example.com
# 连接 ws://localhost:9223 接收实时浏览器视图配置文件
可以创建 agent-browser.json 设置持久化默认值:
json
{
"headed": true,
"proxy": "http://localhost:8080",
"profile": "./browser-data",
"userAgent": "my-agent/1.0",
"ignoreHttpsErrors": true,
"allowedDomains": ["example.com", "*.example.com"]
}优先级:CLI 标志 > 配置文件 > 环境变量
云浏览器支持
Browserbase
bash
export BROWSERBASE_API_KEY="your-key"
export BROWSERBASE_PROJECT_ID="your-id"
agent-browser -p browserbase open https://example.comBrowser Use
bash
export BROWSER_USE_API_KEY="your-key"
agent-browser -p browseruse open https://example.comKernel
bash
export KERNEL_API_KEY="your-key"
agent-browser -p kernel open https://example.com性能对比
| 特性 | Playwright (Node.js) | Agent Browser (Rust) |
|---|---|---|
| CLI 解析延迟 | ~10ms | <1ms |
| 内存占用 | ~150MB (Node.js) | ~30MB (Rust) |
| 启动速度 | 需要每次重新启动 | Daemon 持久化,瞬间响应 |
| 交互延迟 | ~50ms | ~20ms |
适用场景
- ✅ AI Agent 工具集成:Claude Code、Cursor、GitHub Copilot、Windsurf
- ✅ 自动化测试:AI 驱动的端到端测试
- ✅ 数据采集:AI 理解页面,自动提取数据
- ✅ 自动化工作流:AI 决策,浏览器执行
- ✅ CI/CD 自动化:轻量级,适合 serverless
- ✅ Web 自动化:表单填充、点击、截图等
总结
Agent Browser 是一个为 AI Agent 量身定制的浏览器自动化工具。它的核心优势在于:
- 性能极致:Rust 原生二进制,毫秒级响应
- AI 友好:snapshot + refs 工作流专为 LLM 设计
- 安全可靠:默认关闭所有敏感功能,需显式启用
- 生态完整:直接适配主流 AI 工具和云浏览器服务
- 灵活强大:支持本地、云、iOS 多种部署方式
如果你在开发 AI Agent,需要浏览器自动化能力,Agent Browser 绝对是最佳选择!