Agent Browser
为 AI Agent 优化的无头浏览器自动化 CLI 工具,支持可访问性树快照和基于 ref 的元素选择。
这是一个专门用于 AI Agent 的快速浏览器自动化工具,通过可访问性树快照(accessibility tree snapshots)和基于 ref 的元素选择实现确定性操作,适用于自动化多步骤工作流、复杂单页应用(SPA)和需要会话隔离的场景。
核心功能和命令:
导航操作:
agent-browser open - 打开网页 agent-browser back | forward | reload | close - 浏览器导航 快照操作(必须使用 -i --json):
agent-browser snapshot -i --json - 交互元素快照,JSON 输出 agent-browser snapshot -i -c -d 5 --json - 加上紧凑模式和深度限制 agent-browser snapshot -s "#main" -i - 限定选择器范围 交互操作(基于 ref):
agent-browser click @e2 - 点击 agent-browser fill @e3 "text" - 填充 agent-browser type @e3 "text" - 输入 agent-browser hover @e4 - 悬停 agent-browser check @e5 | uncheck @e5 - 复选框 agent-browser select @e6 "value" - 选择 agent-browser press "Enter" - 按键 agent-browser scroll down 500 - 滚动 agent-browser drag @e7 @e8 - 拖拽 获取信息:
agent-browser get text @e1 --json - 获取文本 agent-browser get html @e2 --json - 获取 HTML agent-browser get value @e3 --json - 获取值 agent-browser get attr @e4 "href" --json - 获取属性 agent-browser get title --json - 获取标题 agent-browser get url --json - 获取 URL agent-browser get count ".item" --json - 获取元素数量 状态检查:
agent-browser is visible @e2 --json - 是否可见 agent-browser is enabled @e3 --json - 是否启用 agent-browser is checked @e4 --json - 是否选中 等待操作:
agent-browser wait @e2 - 等待元素 agent-browser wait 1000 - 等待毫秒 agent-browser wait --text "Welcome" - 等待文本 agent-browser wait --url "**/dashboard" - 等待 URL agent-browser wait --load networkidle - 等待网络空闲 agent-browser wait --fn "window.ready === true" - 等待函数返回 true 会话管理(隔离浏览器):
agent-browser --session admin open site.com - 创建管理员会话 AGENT_BROWSER_SESSION=admin agent-browser ... - 通过环境变量设置 状态持久化:
agent-browser state save auth.json - 保存 cookies/storage agent-browser state load auth.json - 加载(跳过登录) 截图和 PDF:
agent-browser screenshot page.png - 截图 agent-browser screenshot --full page.png - 全页截图 agent-browser pdf page.pdf - 导出 PDF 网络控制:
agent-browser network route "/ads/*" --abort - 阻止 agent-browser network route "/api/*" --body '{"x":1}' - 模拟 agent-browser network requests --filter api - 查看 Cookie 和存储:
agent-browser cookies - 获取所有 cookie agent-browser cookies set name value - 设置 cookie agent-browser storage local key - 获取 localStorage agent-browser storage local set key val - 设置 localStorage 标签页和框架:
agent-browser tab new https://example.com - 新建标签页 agent-browser tab 2 - 切换标签页 agent-browser frame @e5 - 切换到 iframe agent-browser frame main - 返回主框架 最佳实践:
始终使用 -i 标志(聚焦交互元素) 始终使用 --json(易于解析) 等待稳定性:agent-browser wait --load networkidle 保存认证状态:state save/load 跳过登录流程 使用会话:隔离不同浏览器上下文 调试时使用 --headed(查看发生了什么)