Community编程与开发github.com

浏览器自动化 (agent-browser)

为 Anthropic 的 Claude Code、Codex、Cursor 等工具提供浏览器自动化插件,支持导航、交互、截图和数据提取。

浏览器自动化 (agent-browser) 是什么?

该技能提供了一个基于 Playwright 的浏览器自动化工具 `agent-browser`,专为 AI 代理(如 Claude Code、Codex、Cursor)设计。核心工作流程包括导航、快照获取(生成元素引用如 `@e1`)、基于引用的交互以及重新快照。支持命令链式调用以在单次 shell 调用中高效完成多步骤操作。主要功能包括表单填写、认证状态持久化(保存/加载 cookies 和 localStorage)、跨页面数据提取、多会话并行管理、连接到现有 Chrome 实例、可视化浏览器调试(含高亮和录制)、iOS 模拟器支持(如 iPhone 16 Pro)以及本地文件处理(PDF/HTML)。差异比较功能可用于验证页面变化。配置通过 `agent-browser.json` 或环境变量进行,会话管理确保并发自动化不会冲突。该技能特别适合需要可靠、可重复浏览器交互的 AI 代理工作流。

兼容平台✓Claude Code✓Codex CLI✓Cursor
npx skills add Jamie-BitFlight/claude_skills

Installed? Explore more 编程与开发 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

使用 agent-browser 进行浏览器自动化

核心工作流

每个浏览器自动化任务都遵循以下模式:

  1. 导航: agent-browser open <url>
  2. 快照: agent-browser snapshot -i(获取元素引用,如 @e1、@e2)
  3. 交互: 使用引用进行点击、填写、选择
  4. 重新快照: 在导航或 DOM 变化后,获取新的引用
agent-browser open https://example.com/form
agent-browser snapshot -i
# 输出:@e1 [input type="email"], @e2 [input type="password"], @e3 [button] "Submit"

agent-browser fill @e1 "[email protected]"
agent-browser fill @e2 "password123"
agent-browser click @e3
agent-browser wait --load networkidle
agent-browser snapshot -i  # 检查结果

命令链式调用

命令可以使用 && 在单个 shell 调用中链式执行。浏览器通过后台守护进程在命令之间保持持久,因此链式调用比单独调用更安全、更高效。

# 在一次调用中链式执行打开 + 等待 + 快照
agent-browser open https://example.com && agent-browser wait --load networkidle && agent-browser snapshot -i

# 链式执行多个交互
agent-browser fill @e1 "[email protected]" && agent-browser fill @e2 "password123" && agent-browser click @e3

# 导航并捕获
agent-browser open https://example.com && agent-browser wait --load networkidle && agent-browser screenshot page.png

何时使用链式调用: 当不需要在继续之前读取中间命令的输出时,使用 &&(例如,打开 + 等待 + 截图)。当需要先解析输出时(例如,快照发现引用,然后使用这些引用进行交互),则单独运行命令。

基本命令

# 导航
agent-browser open <url>              # 导航(别名:goto, navigate)
agent-browser close                   # 关闭浏览器

# 快照
agent-browser snapshot -i             # 带有引用的交互式元素(推荐)
agent-browser snapshot -i -C          # 包含光标交互元素(带有 onclick, cursor:pointer 的 div)
agent-browser snapshot -s "#selector" # 限定 CSS 选择器范围

# 交互(使用快照中的 @ref)
agent-browser click @e1               # 点击元素
agent-browser click @e1 --new-tab     # 点击并在新标签页打开
agent-browser fill @e2 "text"         # 清除并输入文本
agent-browser type @e2 "text"         # 不清除直接输入文本
agent-browser select @e1 "option"     # 选择下拉选项
agent-browser check @e1               # 勾选复选框
agent-browser press Enter             # 按下按键
agent-browser scroll down 500         # 滚动页面

# 获取信息
agent-browser get text @e1            # 获取元素文本
agent-browser get url                 # 获取当前 URL
agent-browser get title               # 获取页面标题

# 等待
agent-browser wait @e1                # 等待元素出现
agent-browser wait --load networkidle # 等待网络空闲
agent-browser wait --url "**/page"    # 等待 URL 模式
agent-browser wait 2000               # 等待指定毫秒数

# 捕获
agent-browser screenshot              # 截图到临时目录
agent-browser screenshot --full       # 全页面截图
agent-browser screenshot --annotate   # 带编号元素标签的注释截图
agent-browser pdf output.pdf          # 保存为 PDF

# 差异比较(比较页面状态)
agent-browser diff snapshot                          # 比较当前与上一次快照
agent-browser diff snapshot --baseline before.txt    # 比较当前与保存的文件
agent-browser diff screenshot --baseline before.png  # 可视化像素差异
agent-browser diff url <url1> <url2>                 # 比较两个页面
agent-browser diff url <url1> <url2> --wait-until networkidle  # 自定义等待策略
agent-browser diff url <url1> <url2> --selector "#main"  # 限定范围

常见模式

表单提交

agent-browser open https://example.com/signup
agent-browser snapshot -i
agent-browser fill @e1 "Jane Doe"
agent-browser fill @e2 "[email protected]"
agent-browser select @e3 "California"
agent-browser check @e4
agent-browser click @e5
agent-browser wait --load networkidle

带状态持久化的身份认证

# 登录一次并保存状态
agent-browser open https://app.example.com/login
agent-browser snapshot -i
agent-browser fill @e1 "$USERNAME"
agent-browser fill @e2 "$PASSWORD"
agent-browser click @e3
agent-browser wait --url "**/dashboard"
agent-browser state save auth.json

# 在后续会话中重用
agent-browser state load auth.json
agent-browser open https://app.example.com/dashboard

会话持久化

# 跨浏览器重启自动保存/恢复 cookies 和 localStorage
agent-browser --session-name myapp open https://app.example.com/login
# ... 登录流程 ...
agent-browser close  # 状态自动保存到 ~/.agent-browser/sessions/

# 下次,状态自动加载
agent-browser --session-name myapp open https://app.example.com/dashboard

# 加密状态
# 在首次使用前配置加密密钥
export AGENT_BROWSER_ENCRYPTION_KEY=$(openssl rand -hex 32)
agent-browser --session-name secure open https://app.example.com

# 管理已保存的状态
agent-browser state list
agent-browser state show myapp-default.json
agent-browser state clear myapp
agent-browser state clean --older-than 7

数据提取

agent-browser open https://example.com/products
agent-browser snapshot -i
agent-browser get text @e5           # 获取特定元素文本
agent-browser get text body > page.txt  # 获取所有页面文本

# JSON 输出用于解析
agent-browser snapshot -i --json
agent-browser get text @e1 --json

并行会话

agent-browser --session site1 open https://site-a.com
agent-browser --session site2 open https://site-b.com

agent-browser --session site1 snapshot -i
agent-browser --session site2 snapshot -i

agent-browser session list

连接到现有 Chrome

# 自动发现已启用远程调试的 Chrome
agent-browser --auto-connect open https://example.com
agent-browser --auto-connect snapshot

# 或使用显式 CDP 端口
agent-browser --cdp 9222 snapshot

可视化浏览器(调试)

agent-browser --headed open https://example.com
agent-browser highlight @e1          # 高亮元素
agent-browser record start demo.webm # 录制会话
agent-browser profiler start         # 启动 Chrome DevTools 性能分析
agent-browser profiler stop trace.json # 停止并保存性能分析(路径可选)

本地文件(PDF、HTML)

# 使用 file:// URL 打开本地文件
agent-browser --allow-file-access open file:///path/to/document.pdf
agent-browser --allow-file-access open file:///path/to/page.html
agent-browser screenshot output.png

iOS 模拟器(移动 Safari)

# 列出可用的 iOS 模拟器
agent-browser device list

# 在特定设备上启动 Safari
agent-browser -p ios --device "iPhone 16 Pro" open https://example.com

# 与桌面相同的工作流——快照、交互、重新快照
agent-browser -p ios snapshot -i
agent-browser -p ios tap @e1          # 轻点(点击的别名)
agent-browser -p ios fill @e2 "text"
agent-browser -p ios swipe up         # 移动端特定手势

# 截图
agent-browser -p ios screenshot mobile.png

# 关闭会话(关闭模拟器)
agent-browser -p ios close

要求: macOS 系统,安装 Xcode 和 Appium(npm install -g appium && appium driver install xcuitest)

真实设备: 如果预先配置,可与物理 iOS 设备一起使用。使用 --device "<UDID>",其中 UDID 来自 xcrun xctrace list devices。

差异比较(验证更改)

在执行操作后使用 diff snapshot 来验证操作是否产生了预期效果。此命令将当前无障碍树与会话中最后一次快照进行比较。

# 典型工作流:快照 -> 操作 -> 差异比较
agent-browser snapshot -i          # 获取基线快照
agent-browser click @e2            # 执行操作
agent-browser diff snapshot        # 查看变化(自动与最后一次快照比较)

对于视觉回归测试或监控:

# 保存基线截图,然后稍后比较
agent-browser screenshot baseline.png
# ... 时间流逝或进行更改 ...
agent-browser diff screenshot --baseline baseline.png

# 比较预发布版与生产版
agent-browser diff url https://staging.example.com https://prod.example.com --screenshot

diff snapshot 输出使用 + 表示添加,- 表示删除,类似于 git diff。diff screenshot 生成一个差异图像,用红色高亮显示更改的像素,并显示不匹配百分比。

超时与慢页面

本地浏览器的默认 Playwright 超时为 60 秒。对于慢速网站或大型页面,请使用显式等待代替依赖默认超时:

# 等待网络活动稳定(最适合慢速页面)
agent-browser wait --load networkidle

# 等待特定元素出现
agent-browser wait "#content"
agent-browser wait @e1

# 等待特定 URL 模式(在重定向后很有用)
agent-browser wait --url "**/dashboard"

# 等待 JavaScript 条件
agent-browser wait --fn "document.readyState === 'complete'"

# 等待固定时长(毫秒),作为最后手段
agent-browser wait 5000

当处理持续慢速的网站时,在 open 之后使用 wait --load networkidle 以确保页面完全加载后再进行快照。如果特定元素渲染慢,可以直接使用 wait <selector> 或 wait @ref 等待它。

会话管理与清理

当同时运行多个代理或自动化任务时,始终使用命名会话以避免冲突:

# 每个代理获得独立的隔离会话
agent-browser --session agent1 open site-a.com
agent-browser --session agent2 open site-b.com

# 检查活动会话
agent-browser session list

完成后始终关闭浏览器会话,以避免进程泄漏:

agent-browser close                    # 关闭默认会话
agent-browser --session agent1 close   # 关闭特定会话

如果之前的会话没有正确关闭,守护进程可能仍在运行。使用 agent-browser close 在开始新工作前进行清理。

引用生命周期(重要)

当页面发生变化时,引用(@e1、@e2 等)将失效。在以下情况后始终重新快照:

  • 点击导航的链接或按钮
  • 表单提交
  • 动态内容加载(下拉菜单、模态框)
agent-browser click @e5              # 导航到新页面
agent-browser snapshot -i            # 必须重新快照
agent-browser click @e1              # 使用新的引用

注释截图(视觉模式)

使用 --annotate 拍摄带有覆盖在交互元素上的编号标签的截图。每个标签 [N] 映射到引用 @eN。这还会缓存引用,因此无需单独的快照即可立即与元素交互。

agent-browser screenshot --annotate
# 输出包括图像路径和图例:
#   [1] @e1 button "Submit"
#   [2] @e2 link "Home"
#   [3] @e3 textbox "Email"
agent-browser click @e2              # 使用注释截图中的引用进行点击

在以下情况下使用注释截图:

  • 页面有未标记的图标按钮或仅视觉元素
  • 需要验证视觉布局或样式
  • 存在 Canvas 或图表元素(文本快照不可见)
  • 需要对元素位置进行空间推理

语义定位器(引用的替代方案)

当引用不可用或不可靠时,使用语义定位器:

agent-browser find text "Sign In" click
agent-browser find label "Email" fill "[email protected]"
agent-browser find role button click --name "Submit"
agent-browser find placeholder "Search" type "query"
agent-browser find testid "submit-btn" click

JavaScript 评估(eval)

使用 eval 在浏览器上下文中运行 JavaScript。Shell 引用可能会破坏复杂表达式——使用 --stdin 或 -b 可避免问题。

# 简单表达式可以使用常规引用
agent-browser eval 'document.title'
agent-browser eval 'document.querySelectorAll("img").length'

# 复杂 JS:使用 --stdin 配合 heredoc(推荐)
agent-browser eval --stdin <<'EVALEOF'
JSON.stringify(
  Array.from(document.querySelectorAll("img"))
    .filter(i => !i.alt)
    .map(i => ({ src: i.src.split("/").pop(), width: i.width }))
)
EVALEOF

# 替代方案:base64 编码(避免所有 shell 转义问题)
agent-browser eval -b "$(echo -n 'Array.from(document.querySelectorAll("a")).map(a => a.href)' | base64)"

为什么这很重要: 当 shell 处理您的命令时,内部的双引号、! 字符(历史扩展)、反引号和 $() 都可能在 JavaScript 到达 agent-browser 之前将其破坏。--stdin 和 -b 标志完全绕过 shell 解释。

经验法则:

  • 单行,无嵌套引号 -> 使用单引号的常规 eval 'expression' 即可
  • 嵌套引号、箭头函数、模板字面量或多行 -> 使用 eval --stdin <<'EVALEOF'
  • 程序化/生成的脚本 -> 使用带 base64 的 eval -b

配置文件

在项目根目录创建 agent-browser.json 以实现持久设置:

{
  "headed": true,
  "proxy": "http://localhost:8080",
  "profile": "./browser-data"
}

优先级(从低到高):~/.agent-browser/config.json < ./agent-browser.json < 环境变量 < CLI 标志。使用 --config <path> 或 AGENT_BROWSER_CONFIG 环境变量指定自定义配置文件(如果文件缺失或无效,则以错误退出)。所有 CLI 选项映射为 camelCase 键(例如,--executable-path -> "executablePath")。布尔标志接受 true/false 值(例如,--headed false 会覆盖配置)。来自用户和项目配置的扩展会合并,而非替换。

深入文档

参考文档使用场景
references/commands.md完整命令参考,包含所有选项
[references/

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

Jamie-BitFlight/agentskill-kaizen-meta-docs

Agentskill kaizen plugin documentation index. Load when needing to read about cross-platform notes, improvement plans, or DuckDB integration.

Jamie-BitFlight/bash-51-features

Bash 5.1 release features and improvements with practical examples. Use when working with Bash 5.1 features, epoch time variables, redirection enhancements, or when user asks about Bash 5.1 changes, new features, or version-specific capabilities.

Jamie-BitFlight/bash-52-features

Bash 5.2 release features and improvements with practical examples. Use when working with Bash 5.2 features, variable handling enhancements, readline improvements, or when user asks about Bash 5.2 changes, new features, or version-specific capabilities.

Jamie-BitFlight/bash-53-features

Bash 5.3 release features and improvements with practical examples. Use when working with Bash 5.3 features, new command substitution, GLOBSORT, loadable builtins, or when user asks about Bash 5.3 changes, new features, or version-specific capabilities.

Jamie-BitFlight/bash-development

This skill should be used when the user asks to "write a bash script", "create a shell script", "implement bash function", "parse arguments in bash", "handle errors in bash", or mentions bash development, shell scripting, script templates, or modern bash patterns.

Jamie-BitFlight/bash-lint

This skill should be used when the user asks to "lint bash script", "run shellcheck", "format shell script", "use shfmt", "fix shellcheck errors", or mentions shell script linting, formatting, code quality, or pre-commit hooks for bash.

Jamie-BitFlight/bash-logging

This skill should be used when the user asks to "add logging to bash script", "colorize output", "implement log levels", "CI/CD sections", "terminal colors in bash", or mentions logging functions, emoji output, collapsible CI sections, or shlocksmith.

Jamie-BitFlight/bash-portability

This skill should be used when the user asks about "POSIX compatibility", "portable shell scripts", "cross-shell compatibility", "bashisms", "shebang selection", or mentions writing scripts that work on different shells (bash, sh, dash, zsh) or different systems.

Jamie-BitFlight/bash-testing

This skill should be used when the user asks to "test bash script", "write shell tests", "use shunit2", "use shellspec", "create test suite for bash", or mentions unit testing, test frameworks, mocking, or test-driven development for shell scripts.

Jamie-BitFlight/brainstorming-skill

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or when users request help with ideation, marketing, and strategic planning. Explores user intent, requirements, and design before implementation using research-validated prompt patterns.

Jamie-BitFlight/clang-format

Configure clang-format code formatting. Use when: user mentions clang-format or .clang-format, analyzing code style/patterns, creating/modifying formatting config, troubleshooting formatting, brace styles/indentation/spacing/alignment/pointer alignment, or codifying conventions.

Jamie-BitFlight/commitlint

When setting up commit message validation for a project. When project has commitlint.config.js or .commitlintrc files. When configuring CI/CD to enforce commit format. When extracting commit rules for LLM prompt generation. When debugging commit message rejection errors.

Jamie-BitFlight/conventional-commits

When writing a git commit message. When task completes and changes need committing. When project uses semantic-release, commitizen, git-cliff. When choosing between feat/fix/chore/docs types. When indicating breaking changes. When generating changelogs from commit history.

Jamie-BitFlight/dasel-reference

Use when querying, modifying, or converting JSON, YAML, TOML, XML, CSV, HCL, or INI with dasel v3. Complete reference for selectors, functions, conditionals, variables, spread operator, type casting, and format-specific patterns.

Jamie-BitFlight/data-exploration

Use when exploring unknown structured data files with dasel v3 — discover schema, list keys, find nested values, sample arrays, identify data types across JSON, YAML, TOML, XML, CSV, HCL, INI formats

Jamie-BitFlight/data-transformation

Use when modifying, converting, or transforming structured data with dasel v3 — in-place mutations, format conversion, batch operations, array manipulation, object construction, and merge patterns across JSON, YAML, TOML, XML, CSV, HCL, INI

Jamie-BitFlight/delegate

Decompose substantive work into phases, dispatch each phase to a sub-agent, and adjudicate what comes back. Use whenever a request asks for implementation, investigation, a fix, a review, or any change to files — including small ones — and whenever you are about to read source or run a diagnostic yourself instead of handing it off. Also use when a report from a sub-agent needs judging, when a phase needs re-dispatching, or when a user names one instance of a pattern. Does not apply when your own prompt begins "Your ROLE_TYPE is sub-agent." — then follow references/sub-agent-contract.md instead.

Jamie-BitFlight/enterprise-hibernate-hbm

Dasel v3 query patterns for Hibernate .hbm.xml mapping files — entity-table binding, Java property-to-column extraction, one-to-many set/list/bag relationship tracing, many-to-one foreign key discovery, batch scanning across 60+ HBM files. Use when querying Hibernate ORM class mappings, extracting schema metadata from Java persistence layer, or auditing entity-column relationships in enterprise legacy codebases.

Jamie-BitFlight/enterprise-installanywhere

Dasel v3 query patterns for InstallAnywhere .iap_xml installer definitions — use when querying action sequences, discovering variables, resolving platform conditions, navigating panels, or comparing installer variants. Files are 2.5+ MB, 65,000+ lines — too large for context reads, requires structural dasel queries.

Jamie-BitFlight/enterprise-maven-pom

Dasel v3 selector patterns for Maven POM XML files — use when querying dependency versions, filtering by groupId or scope, extracting module hierarchy from parent POMs, or detecting version conflicts across enterprise multi-module Java projects. Load this skill when working with pom.xml files using dasel.

相关技能