> [!NOTE] > 本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。 > [English](./README.en.md) · [原始项目](https://github.com/vercel-labs/agent-browser) · [上游 README](https://github.com/vercel-labs/agent-browser/blob/HEAD/README.md) > 原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。 # agent-browser 面向 AI 智能体的浏览器自动化 CLI。原生 Rust CLI,运行快速。 [![skills.sh](https://skills.sh/b/vercel-labs/agent-browser)](https://skills.sh/vercel-labs/agent-browser) ## 安装 ### 全局安装(推荐) 安装原生 Rust 二进制文件: ```bash npm install -g agent-browser agent-browser install # Download Chrome from Chrome for Testing (first time only) ``` ### 项目安装(本地依赖) 适用于希望在 `package.json` 中固定版本的项目: ```bash npm install agent-browser agent-browser install ``` 随后可通过 `package.json` 脚本使用,或直接调用 `agent-browser`。 ### Homebrew(macOS) ```bash brew install agent-browser agent-browser install # Download Chrome from Chrome for Testing (first time only) ``` ### Cargo(Rust) ```bash cargo install agent-browser agent-browser install # Download Chrome from Chrome for Testing (first time only) ``` ### 从源码构建 需要 Node.js 24+、pnpm 11+ 和 Rust。 ```bash git clone https://github.com/vercel-labs/agent-browser cd agent-browser pnpm install pnpm build pnpm build:native # Requires Rust (https://rustup.rs) pnpm link --global # Makes agent-browser available globally agent-browser install ``` ### Linux 依赖 在 Linux 上,请安装系统依赖: ```bash agent-browser install --with-deps ``` 如果包管理器无法安装所有必需的浏览器库,进程将以非零状态退出。 ### 更新 升级到最新版本: ```bash agent-browser upgrade ``` 自动检测你的安装方式(npm、Homebrew 或 Cargo),并运行相应的更新命令。 ### 系统要求 - **Chrome** — 运行 `agent-browser install` 从 [Chrome for Testing](https://developer.chrome.com/blog/chrome-for-testing/)(Google 官方自动化渠道)下载 Chrome。现有的 Chrome、Brave、Playwright 和 Puppeteer 安装会被自动检测。守护进程(daemon)无需 Playwright 或 Node.js。 - **Node.js 24+ 和 pnpm 11+** — 仅在从源码构建时需要。 - **Rust** — 仅在从源码构建时需要(参见上文「从源码构建」)。 ## 快速入门 ```bash agent-browser open example.com agent-browser snapshot # Get accessibility tree with refs agent-browser click @e2 # Click by ref from snapshot agent-browser fill @e3 "test@example.com" # Fill by ref agent-browser get text @e1 # Get text by ref agent-browser screenshot page.png agent-browser close ``` 当其他元素遮挡目标的点击位置时(例如同意横幅或模态框),点击会提前失败。请先关闭或与报告中的遮挡元素交互,然后重新获取快照,再重试原始 ref。 无头(Headless)Chromium 截图会隐藏原生滚动条,以保证图像输出一致。启动时传入 `--hide-scrollbars false` 可保留原生滚动条可见。 ### 传统选择器(同样支持) ```bash agent-browser click "#submit" agent-browser fill "#email" "test@example.com" agent-browser find role button click --name "Submit" ``` ## 命令 ### 核心命令 ```bash agent-browser open # Launch browser (no navigation); stays on about:blank agent-browser open # Launch + navigate to URL (aliases: goto, navigate) agent-browser read [url] # Fetch agent-readable text, or read rendered active-tab DOM agent-browser click # Click element (--new-tab to open in new tab) agent-browser dblclick # Double-click element agent-browser focus # Focus element agent-browser type # Type into element agent-browser fill # Clear and fill agent-browser press # Press key (Enter, Tab, Control+a) (alias: key) agent-browser keyboard type # Type with real keystrokes (no selector, current focus) agent-browser keyboard inserttext # Insert text without key events (no selector) agent-browser keydown # Hold key down agent-browser keyup # Release key agent-browser hover # Hover element agent-browser select # Select dropdown option agent-browser check # Check checkbox agent-browser uncheck # Uncheck checkbox agent-browser scroll [px] # Scroll (up/down/left/right, --selector ) agent-browser scrollintoview # Scroll element into view (alias: scrollinto) agent-browser drag # Drag and drop agent-browser upload # Upload files agent-browser screenshot [path] # Take screenshot (--full for full page, saves to a temporary directory if no path) agent-browser screenshot --annotate # Annotated screenshot with numbered element labels agent-browser screenshot --screenshot-dir ./shots # Save to custom directory agent-browser screenshot --screenshot-format jpeg --screenshot-quality 80 agent-browser pdf # Save as PDF agent-browser snapshot # Accessibility tree with refs (best for AI) agent-browser eval # Run JavaScript (-b for base64, --stdin for piped input) agent-browser connect # Connect to browser via CDP agent-browser stream enable [--port ] # Start runtime WebSocket streaming agent-browser stream status # Show runtime streaming state and bound port agent-browser stream disable # Stop runtime WebSocket streaming agent-browser close # Close browser (aliases: quit, exit) agent-browser close --all # Close all active sessions agent-browser chat "" # AI chat: natural language browser control (single-shot) agent-browser chat # AI chat: interactive REPL mode ``` ### 获取信息 ```bash agent-browser get text # Get text content agent-browser get html # Get innerHTML agent-browser get value # Get input value agent-browser get attr # Get attribute agent-browser get title # Get page title agent-browser get url # Get current URL agent-browser get cdp-url # Get CDP WebSocket URL (for DevTools, debugging) agent-browser get count # Count matching elements agent-browser get box # Get bounding box agent-browser get styles # Get computed styles ``` ### 读取对智能体友好的文本 ```bash agent-browser read agent-browser read https://example.com/article agent-browser read https://example.com/article --filter overview agent-browser read https://example.com/article --outline agent-browser read https://docs.example.com --llms index --filter auth agent-browser read https://docs.example.com --llms full --filter auth agent-browser read example.com/article --require-md agent-browser read https://example.com/article --json ``` `read` 可在不启动 Chrome 的情况下获取 URL。省略 URL 时,会读取当前浏览器会话中活动标签页的渲染后 DOM,包括浏览器认证状态和客户端更新。显式指定 URL 读取时,默认发送 `Accept: text/markdown`;若首次响应不是 markdown,可尝试在同一 URL 后追加 `.md`;沿祖先路径向 `/` 遍历,以查找最近的匹配文档链接 `llms.txt`;在可用时输出 markdown 或纯文本,否则回退到从 HTML 提取的可读文本。未指定 URL 时,`--llms` 和 `--require-md` 使用活动标签页 URL,因为它们依赖 HTTP 资源。除非你明确要求,否则 `read` 不会读取 `llms-full.txt`。 选项:`--raw` 直接打印响应体,不进行 HTML 提取;`--require-md` 除非服务器返回 `Content-Type: text/markdown` 否则失败;`--outline` 打印单页的紧凑标题大纲;`--llms index` 打印紧凑的最近祖先 `llms.txt` 链接列表;`--llms full` 读取最近祖先 `llms-full.txt`;`--filter ` 可缩小页面区块、llms 链接/区块或大纲标题范围;`--timeout ` 可更改请求超时。`--allowed-domains`、`--content-boundaries` 和 `--max-output` 等全局保护措施同样适用于读取请求与输出。 ### 检查状态(Check State) ```bash agent-browser is visible # Check if visible agent-browser is enabled # Check if enabled agent-browser is checked # Check if checked ``` ### 查找元素(语义定位器 Semantic Locators) ```bash agent-browser find role [value] # By ARIA role agent-browser find text # By text content agent-browser find label