| name | browser-automation |
| description | Use when debugging web pages or Electron apps with screenshots, element inspection, UI interactions, or style checks. Also use for network monitoring, performance analysis, heap snapshots, or accessibility tree inspection. Not for API testing or backend debugging. |
Browser Automation
Overview
优先 Playwright,特殊场景回退 CDP。 同一个 CDP 连接,两套工具按需切换。Playwright 提供自动等待和智能选择器,减少 flaky 操作;CDP 提供网络监控、性能分析等底层能力。
When NOT to Use
- API/后端调试 → 用 HTTP 客户端或后端日志工具
- Playwright 测试框架(
npx playwright test)→ 用 Playwright CLI,不需要本 skill
- 用户未运行 Chrome/Electron 且未同意新开 → 不能启动浏览器
选择策略
| 场景 | 工具 | 原因 |
|---|
| 截图、点击、填写、导航 | Playwright | 自动等待、智能选择器、一键样式检查 |
| DOM 快照 | Playwright | 结构化 JSON,含 CSS class |
| 网络请求监控 | CDP | Playwright 连接模式无法拦截已有上下文请求 |
| 控制台日志捕获 | CDP | 需持久 WebSocket 接收事件流 |
| Performance/Profiler | CDP | Playwright 不封装底层性能 API |
| Heap Snapshot | CDP | Playwright 不提供内存分析 |
| Accessibility Tree | CDP | 需浏览器原生 a11y 树(含 disabled/checked) |
| 对话框处理、文件上传 | CDP | Playwright skill 暂未实现 |
前置条件
cd ~/.pi/agent/skills/browser-automation/scripts && npm install
⚠️ 进程管理规范
规则 1:明确用户意图
- "看看我的 xxx 应用" → 连接已有进程
- "打开一个新页面" → 新开进程
- 不确定 → 问用户
规则 2:连接已有进程(优先)
lsof -i :9222 2>/dev/null
node scripts/pw.js http://localhost:9222 list-pages
规则 3:新开进程(仅在需要时)
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
--remote-debugging-port=9222 \
--user-data-dir=/tmp/browser-profile &
CHROME_PID=$!
规则 4:精准关闭
❌ pkill chrome → 会杀掉用户所有 Chrome 窗口,包括正在工作的标签页
❌ pkill electron / pkill -f "vite" → 同上,误杀无关进程
✅ kill $PID($! 或 lsof -ti :9222 获取)— 只杀你启动的那个进程
命令速查
EP="http://localhost:9222"
PW="node ~/.pi/agent/skills/browser-automation/scripts/pw.js"
CDP="node ~/.pi/agent/skills/browser-automation/scripts/cdp.js"
WS="$(curl -s $EP/json/list | python3 -c 'import sys,json; print(json.load(sys.stdin)[0]["webSocketDebuggerUrl"])')"
Playwright(日常操作)
$PW $EP list-pages
$PW $EP select-page 0
$PW $EP navigate "https://example.com"
$PW $EP screenshot
$PW $EP screenshot -s ".user-list" -o user-list.png
$PW $EP snapshot
$PW $EP snapshot full
$PW $EP click "text=提交"
$PW $EP dblclick ".row"
$PW $EP click "text=菜单" --button right
$PW $EP fill "input[name=email]" "test@x.com"
$PW $EP type "input[name=search]" "关键词"
$PW $EP select "select.country" "CN"
$PW $EP check "input[type=checkbox]"
$PW $EP uncheck "input[type=checkbox]"
$PW $EP hover ".dropdown-trigger"
$PW $EP press Enter
$PW $EP wait ".loaded-content"
$PW $EP wait ".loading-mask" hidden
$PW $EP wait "text=操作成功" 5000
$PW $EP scroll down 500
$PW $EP go-back
$PW $EP text "h1"
$PW $EP html ".user-card"
$PW $EP styles ".user-card"
$PW $EP evaluate "document.title"
选择器:CSS、text=、role=、label=、placeholder=、testid=、alt=
CDP(底层调试)
$CDP "$WS" Network.enable '{}'
$CDP "$WS" Runtime.enable '{}'
$CDP "$WS" Performance.enable '{}'
$CDP "$WS" HeapProfiler.takeHeapSnapshot '{}'
$CDP "$WS" Accessibility.getFullAXTree '{}'
$CDP "$WS" Page.handleJavaScriptDialog '{"action":"accept"}'
$CDP "$WS" Page.captureScreenshot '{"format":"png"}'
详细命令参考:
- Playwright 完整命令 →
references/playwright-commands.md
- CDP 完整命令 →
references/cdp-commands.md
Common Mistakes
| 错误 | 后果 | 修复 |
|---|
pkill chrome | 杀掉用户所有窗口 | kill $PID(精准) |
| 不检测端口直接启动 | 端口冲突,连接失败 | 先 lsof -i :9222 |
| 用完不关进程 | 后台残留 Chrome | 用完立即 kill $PID |
| 把 CDP 当首选 | 手动轮询、代码冗长 | 日常操作用 Playwright |
Electron 支持
Electron 支持 --remote-debugging-port,Playwright 直接连接:
npx electron . --remote-debugging-port=9222
$PW $EP list-pages