跳转至

快速入门

核心工作流

每次浏览器自动化操作都遵循以下模式:

# 1. Navigate
agent-browser open example.com

# 2. Snapshot to get element refs
agent-browser snapshot -i
# Output:
# @e1 [heading] "Example Domain"
# @e2 [link] "More information..."

# 3. Interact using refs
agent-browser click @e2

# 4. Re-snapshot after page changes
agent-browser snapshot -i

常用命令

agent-browser open example.com
agent-browser snapshot -i                # Get interactive elements with refs
agent-browser click @e2                  # Click by ref
agent-browser fill @e3 "test@example.com" # Fill input by ref
agent-browser get text @e1               # Get text content
agent-browser screenshot                 # Save to temp directory
agent-browser screenshot page.png        # Save to specific path
agent-browser close

传统选择器

同时支持 CSS 选择器和语义定位器:

agent-browser click "#submit"
agent-browser fill "#email" "test@example.com"
agent-browser find role button click --name "Submit"

有头模式

显示浏览器窗口以进行调试:

agent-browser open example.com --headed

等待内容

agent-browser wait @e1                   # Wait for element
agent-browser wait --text "Welcome"      # Wait for expected text
agent-browser wait --url "**/dashboard"  # Wait for URL pattern
agent-browser wait --fn "window.appReady === true" # Wait for JS condition
agent-browser wait --load domcontentloaded # Wait for the DOM lifecycle event

页面变化后,请优先选择代表所需结果的条件。仅当生命周期事件是关键节点时,才使用 load 或 domcontentloaded。对于已知会进入静默状态的页面,networkidle 仍然可用,但 SSE、WebSocket、轮询和长轮询可能会使其无法完成解析。

命令链式调用

在单次 shell 调用中使用 && 链式调用命令。浏览器通过后台守护进程保持运行,因此链式调用既安全又高效:

# Open, wait, and snapshot in one call
agent-browser open example.com && agent-browser wait --load domcontentloaded && agent-browser snapshot -i

# Chain multiple interactions
agent-browser fill @e1 "user@example.com" && agent-browser fill @e2 "pass" && agent-browser click @e3

# Navigate and capture
agent-browser open example.com && agent-browser wait --load load && agent-browser screenshot page.png

当不需要中间输出时,使用 &&。当需要先解析输出时(例如,先通过 snapshot 发现引用,再进行交互),请单独运行命令。

JSON 输出

用于在脚本中进行程序化解析:

agent-browser snapshot --json
agent-browser get text @e1 --json

注意:默认的文本输出更简洁,更适合 AI 智能体使用。

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 agen/agent-browser