chrome-devtools-axi-browser-automation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesechrome-devtools-axi Browser Automation
chrome-devtools-axi 浏览器自动化
Overview
概述
chrome-devtools-axichrome-devtools-mcpKey features:
- Accessibility tree snapshots with refs for precise element targeting
uid= - Generation-based staleness detection prevents silent no-ops when page state changes
- Persistent bridge process keeps Chrome session alive across commands
- Combined operations reduce round-trips for agents
chrome-devtools-axichrome-devtools-mcp核心特性:
- 可访问性树快照:通过引用实现精准元素定位
uid= - 基于生成版本的过期检测:页面状态变化时避免无提示的无效操作
- 持久化桥接进程:跨命令保持Chrome会话持续活跃
- 组合操作:减少Agent的往返请求次数
Installation
安装
For agents to install automatically:
bash
npx -y chrome-devtools-axiFor manual installation:
bash
npm install -g chrome-devtools-axiTell your agent:
Execute `npx -y chrome-devtools-axi` to get browser automation tools.供Agent自动安装:
bash
npx -y chrome-devtools-axi手动安装:
bash
npm install -g chrome-devtools-axi告知你的Agent:
执行 `npx -y chrome-devtools-axi` 获取浏览器自动化工具。Architecture
架构
The CLI communicates with a persistent bridge server (localhost:9224 by default) that maintains an MCP session with Chrome via DevTools Protocol. The bridge auto-starts on first command and writes state to .
~/.chrome-devtools-axi/CLI与持久化桥接服务器(默认端口localhost:9224)通信,该服务器通过DevTools Protocol维持与Chrome的MCP会话。桥接服务会在首次执行命令时自动启动,并将状态写入目录。
~/.chrome-devtools-axi/Core Commands
核心命令
Navigation and Snapshot
导航与快照
bash
undefinedbash
undefinedNavigate to a URL and capture accessibility tree
导航至指定URL并捕获可访问性树
chrome-devtools-axi open https://example.com
chrome-devtools-axi open https://example.com
Capture current page state
捕获当前页面状态
chrome-devtools-axi snapshot
chrome-devtools-axi snapshot
Navigate back
返回上一页
chrome-devtools-axi back
chrome-devtools-axi back
Scroll page
滚动页面
chrome-devtools-axi scroll down
chrome-devtools-axi scroll top
chrome-devtools-axi scroll down
chrome-devtools-axi scroll top
Wait for time or content
等待指定时长或内容加载完成
chrome-devtools-axi wait 2000
chrome-devtools-axi wait "Welcome"
undefinedchrome-devtools-axi wait 2000
chrome-devtools-axi wait "Welcome"
undefinedElement Interaction
元素交互
Elements in snapshots are marked with refs. Always use refs exactly as printed:
uid=g<generation>:<id>bash
undefined快照中的元素会标记为格式的引用,请严格按照输出内容使用这些引用:
uid=g<generation>:<id>bash
undefinedClick an element
点击元素
chrome-devtools-axi click @g1:1
chrome-devtools-axi click @g1:1
Fill a form field
填写表单字段
chrome-devtools-axi fill @g2:5 "user@example.com"
chrome-devtools-axi fill @g2:5 "user@example.com"
Fill multiple fields at once
批量填写多个字段
chrome-devtools-axi fillform @g2:5=john @g2:6=doe @g2:7=john@example.com
chrome-devtools-axi fillform @g2:5=john @g2:6=doe @g2:7=john@example.com
Type text at current focus
在当前焦点位置输入文本
chrome-devtools-axi type "search query"
chrome-devtools-axi type "search query"
Press keyboard key
按下键盘按键
chrome-devtools-axi press Enter
chrome-devtools-axi press Tab
chrome-devtools-axi press Enter
chrome-devtools-axi press Tab
Hover over element
悬停在元素上
chrome-devtools-axi hover @g1:3
chrome-devtools-axi hover @g1:3
Drag and drop
拖拽操作
chrome-devtools-axi drag @g1:4 @g1:8
chrome-devtools-axi drag @g1:4 @g1:8
Upload file
上传文件
chrome-devtools-axi upload @g1:2 /path/to/file.pdf
chrome-devtools-axi upload @g1:2 /path/to/file.pdf
Handle browser dialogs
处理浏览器对话框
chrome-devtools-axi dialog accept
chrome-devtools-axi dialog dismiss
undefinedchrome-devtools-axi dialog accept
chrome-devtools-axi dialog dismiss
undefinedScreenshots
截图
bash
undefinedbash
undefinedSave screenshot to file
将截图保存至文件
chrome-devtools-axi screenshot output.png
chrome-devtools-axi screenshot output.png
Capture specific element
捕获指定元素的截图
chrome-devtools-axi screenshot element.png --uid @g1:5
chrome-devtools-axi screenshot element.png --uid @g1:5
Full-page screenshot
全页面截图
chrome-devtools-axi screenshot full.png --full-page
chrome-devtools-axi screenshot full.png --full-page
Specify format
指定截图格式
chrome-devtools-axi screenshot output.jpg --format jpeg
undefinedchrome-devtools-axi screenshot output.jpg --format jpeg
undefinedJavaScript Evaluation
JavaScript 执行
bash
undefinedbash
undefinedEvaluate expression (auto-wrapped)
执行表达式(自动包装)
chrome-devtools-axi eval "document.title"
chrome-devtools-axi eval "document.title"
Execute function
执行函数
chrome-devtools-axi eval "() => document.querySelectorAll('a').length"
chrome-devtools-axi eval "() => document.querySelectorAll('a').length"
Multi-statement logic
多语句逻辑执行
chrome-devtools-axi eval "() => {
const rows = [...document.querySelectorAll('tr')];
return rows.map(row => row.textContent.trim());
}"
undefinedchrome-devtools-axi eval "() => {
const rows = [...document.querySelectorAll('tr')];
return rows.map(row => row.textContent.trim());
}"
undefinedMulti-Step Scripts
多步骤脚本
bash
undefinedbash
undefinedExecute script from stdin
从标准输入执行脚本
cat << 'EOF' | chrome-devtools-axi run
open https://example.com
wait "Example Domain"
click @g1:1
snapshot
EOF
undefinedcat << 'EOF' | chrome-devtools-axi run
open https://example.com
wait "Example Domain"
click @g1:1
snapshot
EOF
undefinedPage Management
页面管理
bash
undefinedbash
undefinedList all tabs
列出所有标签页
chrome-devtools-axi pages
chrome-devtools-axi pages
Open new tab
打开新标签页
chrome-devtools-axi newpage https://google.com
chrome-devtools-axi newpage https://google.com
Open in background
在后台打开新标签页
chrome-devtools-axi newpage https://github.com --background
chrome-devtools-axi newpage https://github.com --background
Switch tab
切换标签页
chrome-devtools-axi selectpage <id>
chrome-devtools-axi selectpage <id>
Close tab
关闭标签页
chrome-devtools-axi closepage <id>
chrome-devtools-axi closepage <id>
Resize viewport
调整视口大小
chrome-devtools-axi resize 1920 1080
undefinedchrome-devtools-axi resize 1920 1080
undefinedDevice Emulation
设备模拟
bash
undefinedbash
undefinedEmulate mobile viewport
模拟移动端视口
chrome-devtools-axi emulate --viewport "390x844x3,mobile"
chrome-devtools-axi emulate --viewport "390x844x3,mobile"
Set color scheme
设置配色方案
chrome-devtools-axi emulate --color-scheme dark
chrome-devtools-axi emulate --color-scheme dark
Network throttling
网络节流
chrome-devtools-axi emulate --network "Slow 3G"
chrome-devtools-axi emulate --network "Slow 3G"
CPU throttling (1-20)
CPU节流(1-20)
chrome-devtools-axi emulate --cpu 4
chrome-devtools-axi emulate --cpu 4
Set geolocation
设置地理位置
chrome-devtools-axi emulate --geolocation "37.7749x-122.4194"
chrome-devtools-axi emulate --geolocation "37.7749x-122.4194"
Custom user agent
自定义用户代理
chrome-devtools-axi emulate --user-agent "CustomBot/1.0"
chrome-devtools-axi emulate --user-agent "CustomBot/1.0"
Combine options
组合多个模拟选项
chrome-devtools-axi emulate --viewport "390x844x3,mobile" --color-scheme dark --network "Fast 3G"
undefinedchrome-devtools-axi emulate --viewport "390x844x3,mobile" --color-scheme dark --network "Fast 3G"
undefinedDevTools Debugging
DevTools 调试
Console Messages
控制台消息
bash
undefinedbash
undefinedList all console messages
列出所有控制台消息
chrome-devtools-axi console
chrome-devtools-axi console
Filter by type
按类型过滤
chrome-devtools-axi console --type error
chrome-devtools-axi console --type warn
chrome-devtools-axi console --type error
chrome-devtools-axi console --type warn
Pagination
分页显示
chrome-devtools-axi console --limit 10 --page 2
chrome-devtools-axi console --limit 10 --page 2
Get specific message
获取指定消息
chrome-devtools-axi console-get <id>
Available types: `log`, `debug`, `info`, `error`, `warn`, `dir`, `dirxml`, `table`, `trace`, `clear`, `assert`, `all`chrome-devtools-axi console-get <id>
支持的类型:`log`, `debug`, `info`, `error`, `warn`, `dir`, `dirxml`, `table`, `trace`, `clear`, `assert`, `all`Network Requests
网络请求
bash
undefinedbash
undefinedList network requests
列出网络请求
chrome-devtools-axi network
chrome-devtools-axi network
Filter by type
按类型过滤
chrome-devtools-axi network --type xhr
chrome-devtools-axi network --type fetch
chrome-devtools-axi network --type xhr
chrome-devtools-axi network --type fetch
Get specific request
获取指定请求
chrome-devtools-axi network-get <id>
chrome-devtools-axi network-get <id>
Save response body
保存响应体
chrome-devtools-axi network-get <id> --response-file response.json
chrome-devtools-axi network-get <id> --response-file response.json
Save request body
保存请求体
chrome-devtools-axi network-get <id> --request-file request.json
Available types: `document`, `stylesheet`, `image`, `media`, `font`, `script`, `xhr`, `fetch`, `websocket`, `manifest`, `other`, `all`chrome-devtools-axi network-get <id> --request-file request.json
支持的类型:`document`, `stylesheet`, `image`, `media`, `font`, `script`, `xhr`, `fetch`, `websocket`, `manifest`, `other`, `all`Performance
性能分析
Lighthouse Audits
Lighthouse 审计
bash
undefinedbash
undefinedRun Lighthouse audit
运行Lighthouse审计
chrome-devtools-axi lighthouse
chrome-devtools-axi lighthouse
Mobile device
针对移动端设备
chrome-devtools-axi lighthouse --device mobile
chrome-devtools-axi lighthouse --device mobile
Snapshot mode (current state)
快照模式(基于当前页面状态)
chrome-devtools-axi lighthouse --mode snapshot
chrome-devtools-axi lighthouse --mode snapshot
Save reports to directory
将报告保存至指定目录
chrome-devtools-axi lighthouse --output-dir ./reports
undefinedchrome-devtools-axi lighthouse --output-dir ./reports
undefinedPerformance Tracing
性能追踪
bash
undefinedbash
undefinedStart trace
开始追踪
chrome-devtools-axi perf-start
chrome-devtools-axi perf-start
Start without reload
不刷新页面直接开始追踪
chrome-devtools-axi perf-start --no-reload
chrome-devtools-axi perf-start --no-reload
Stop and save trace
停止追踪并保存结果
chrome-devtools-axi perf-stop --file trace.json
chrome-devtools-axi perf-stop --file trace.json
Analyze performance insight
分析性能指标
chrome-devtools-axi perf-insight lcp "Largest Contentful Paint"
undefinedchrome-devtools-axi perf-insight lcp "Largest Contentful Paint"
undefinedHeap Snapshots
堆快照
bash
undefinedbash
undefinedCapture heap snapshot
捕获堆快照
chrome-devtools-axi heap memory.heapsnapshot
undefinedchrome-devtools-axi heap memory.heapsnapshot
undefinedConfiguration
配置
Environment Variables
环境变量
bash
undefinedbash
undefinedChange bridge server port (default: 9224)
修改桥接服务器端口(默认:9224)
export CHROME_DEVTOOLS_AXI_PORT=9225
export CHROME_DEVTOOLS_AXI_PORT=9225
Connect to existing Chrome instance (HTTP/HTTPS)
连接到已有的Chrome实例(HTTP/HTTPS)
export CHROME_DEVTOOLS_AXI_BROWSER_URL=http://127.0.0.1:9222
export CHROME_DEVTOOLS_AXI_BROWSER_URL=http://127.0.0.1:9222
Connect via WebSocket directly
直接通过WebSocket连接
export CHROME_DEVTOOLS_AXI_BROWSER_URL=wss://cluster.example/launch
export CHROME_DEVTOOLS_AXI_BROWSER_URL=wss://cluster.example/launch
WebSocket authentication headers (JSON)
WebSocket认证头(JSON格式)
export CHROME_DEVTOOLS_AXI_WS_HEADERS='{"Authorization":"Bearer token"}'
export CHROME_DEVTOOLS_AXI_WS_HEADERS='{"Authorization":"Bearer token"}'
Disable session hook auto-install
禁用会话钩子自动安装
export CHROME_DEVTOOLS_AXI_DISABLE_HOOKS=1
undefinedexport CHROME_DEVTOOLS_AXI_DISABLE_HOOKS=1
undefinedState Files
状态文件
State stored in :
~/.chrome-devtools-axi/- — Running bridge PID and port
bridge.pid - — Staleness detection counter
snapshot-generation
状态信息存储在目录下:
~/.chrome-devtools-axi/- — 运行中的桥接服务PID和端口
bridge.pid - — 过期检测计数器
snapshot-generation
Common Patterns
常见使用模式
Login Flow
登录流程
bash
undefinedbash
undefinedNavigate and fill login form
导航至登录页面并填写表单
chrome-devtools-axi open https://app.example.com/login
chrome-devtools-axi fillform @g1:1=user@example.com @g1:2=password
chrome-devtools-axi click @g1:3
chrome-devtools-axi wait "Dashboard"
chrome-devtools-axi snapshot
undefinedchrome-devtools-axi open https://app.example.com/login
chrome-devtools-axi fillform @g1:1=user@example.com @g1:2=password
chrome-devtools-axi click @g1:3
chrome-devtools-axi wait "Dashboard"
chrome-devtools-axi snapshot
undefinedForm Scraping
表单数据提取
bash
undefinedbash
undefinedExtract form data with JavaScript
通过JavaScript提取表单数据
chrome-devtools-axi eval "() => {
const form = document.querySelector('form');
const data = new FormData(form);
return Object.fromEntries(data.entries());
}"
undefinedchrome-devtools-axi eval "() => {
const form = document.querySelector('form');
const data = new FormData(form);
return Object.fromEntries(data.entries());
}"
undefinedMulti-Page Workflow
多页面工作流
typescript
// In TypeScript/Node.js
import { execSync } from 'child_process';
function runCommand(cmd: string): string {
return execSync(`chrome-devtools-axi ${cmd}`, { encoding: 'utf-8' });
}
// Navigate and extract data
runCommand('open https://example.com');
const title = runCommand('eval "document.title"');
// Take screenshot
runCommand('screenshot page.png');
// Fill and submit form
runCommand('fill @g1:1 "search term"');
runCommand('press Enter');
runCommand('wait 2000');
// Extract results
const results = runCommand('eval "() => [...document.querySelectorAll(\'.result\')].map(el => el.textContent)"');typescript
// TypeScript/Node.js 示例
import { execSync } from 'child_process';
function runCommand(cmd: string): string {
return execSync(`chrome-devtools-axi ${cmd}`, { encoding: 'utf-8' });
}
// 导航并提取数据
runCommand('open https://example.com');
const title = runCommand('eval "document.title"');
// 截取页面截图
runCommand('screenshot page.png');
// 填写并提交表单
runCommand('fill @g1:1 "search term"');
runCommand('press Enter');
runCommand('wait 2000');
// 提取结果
const results = runCommand('eval "() => [...document.querySelectorAll(\'.result\')].map(el => el.textContent)"');Handling Dynamic Content
动态内容处理
bash
undefinedbash
undefinedWait for element to appear, then interact
等待元素出现后再进行交互
chrome-devtools-axi open https://spa.example.com
chrome-devtools-axi wait "Loading complete"
chrome-devtools-axi snapshot
chrome-devtools-axi click @g2:5
undefinedchrome-devtools-axi open https://spa.example.com
chrome-devtools-axi wait "Loading complete"
chrome-devtools-axi snapshot
chrome-devtools-axi click @g2:5
undefinedTesting Responsive Design
响应式设计测试
bash
undefinedbash
undefinedTest mobile layout
测试移动端布局
chrome-devtools-axi emulate --viewport "390x844x3,mobile"
chrome-devtools-axi open https://example.com
chrome-devtools-axi screenshot mobile.png
chrome-devtools-axi emulate --viewport "390x844x3,mobile"
chrome-devtools-axi open https://example.com
chrome-devtools-axi screenshot mobile.png
Test desktop layout
测试桌面端布局
chrome-devtools-axi resize 1920 1080
chrome-devtools-axi snapshot
chrome-devtools-axi screenshot desktop.png
undefinedchrome-devtools-axi resize 1920 1080
chrome-devtools-axi snapshot
chrome-devtools-axi screenshot desktop.png
undefinedUnderstanding Refs and Staleness
理解引用与过期机制
Refs use a generation prefix () that increments with each snapshot. If you capture a snapshot at generation 1, get ref , then the page re-renders and moves to generation 2, attempting to use will fail with error.
g<N>:@g1:5@g1:5STALE_REFAgent pattern:
- Capture snapshot
- Extract refs
- Attempt action with ref
- If error, re-snapshot and retry with new ref
STALE_REF
bash
undefined引用包含生成版本前缀(),每次快照都会递增版本号。如果在版本1的快照中获取到引用,之后页面重新渲染进入版本2,此时使用会触发错误。
g<N>:@g1:5@g1:5STALE_REFAgent使用模式:
- 捕获快照
- 提取引用
- 使用引用执行操作
- 如果触发错误,重新捕获快照并使用新引用重试
STALE_REF
bash
undefinedFirst snapshot
首次捕获快照
chrome-devtools-axi snapshot
chrome-devtools-axi snapshot
Output shows: uid=g1:1 link "Click here"
输出显示:uid=g1:1 link "Click here"
Page changes, generation bumps to g2
页面更新,版本号升级为g2
chrome-devtools-axi click @g1:1
chrome-devtools-axi click @g1:1
Error: STALE_REF
错误:STALE_REF
Re-snapshot to get fresh refs
重新捕获快照获取最新引用
chrome-devtools-axi snapshot
chrome-devtools-axi snapshot
Output shows: uid=g2:3 link "Click here"
输出显示:uid=g2:3 link "Click here"
chrome-devtools-axi click @g2:3
chrome-devtools-axi click @g2:3
Success
操作成功
undefinedundefinedTroubleshooting
故障排查
Bridge Not Starting
桥接服务无法启动
bash
undefinedbash
undefinedManually stop and restart bridge
手动停止并重启桥接服务
chrome-devtools-axi stop
chrome-devtools-axi start
undefinedchrome-devtools-axi stop
chrome-devtools-axi start
undefinedPort Conflicts
端口冲突
bash
undefinedbash
undefinedUse different port
使用其他端口
export CHROME_DEVTOOLS_AXI_PORT=9999
chrome-devtools-axi open https://example.com
undefinedexport CHROME_DEVTOOLS_AXI_PORT=9999
chrome-devtools-axi open https://example.com
undefinedStale Refs
引用过期
Always use refs exactly as printed in snapshot output. If you get , capture a new snapshot.
STALE_REF请严格按照快照输出内容使用引用。如果出现错误,重新捕获快照即可。
STALE_REFElement Not Found
元素未找到
If is valid but element doesn't respond:
uid- Check if element is visible (scroll into view)
- Wait for page to finish loading
- Use before interaction
chrome-devtools-axi wait
如果有效但无响应:
uid- 检查元素是否可见(可滚动至视图内)
- 等待页面加载完成
- 交互前使用命令
chrome-devtools-axi wait
Full Output Needed
需要完整输出
bash
undefinedbash
undefinedDisable truncation for complete data
禁用截断以获取完整数据
chrome-devtools-axi snapshot --full
chrome-devtools-axi console --full
undefinedchrome-devtools-axi snapshot --full
chrome-devtools-axi console --full
undefinedDebugging JavaScript Eval
JavaScript执行调试
bash
undefinedbash
undefinedCheck for errors in console
检查控制台中的错误信息
chrome-devtools-axi console --type error
chrome-devtools-axi console --type error
Test simple eval first
先测试简单的执行命令
chrome-devtools-axi eval "2 + 2"
chrome-devtools-axi eval "2 + 2"
Use arrow function for complex logic
复杂逻辑使用箭头函数
chrome-devtools-axi eval "() => { console.log('debug'); return 42; }"
undefinedchrome-devtools-axi eval "() => { console.log('debug'); return 42; }"
undefinedExample: E2E Testing Workflow
示例:端到端测试工作流
bash
#!/bin/bashbash
#!/bin/bashStart fresh session
启动全新会话
chrome-devtools-axi stop
chrome-devtools-axi start
chrome-devtools-axi stop
chrome-devtools-axi start
Navigate to app
导航至应用
chrome-devtools-axi open https://app.example.com
chrome-devtools-axi open https://app.example.com
Login
登录
chrome-devtools-axi fillform @g1:1=$USER_EMAIL @g1:2=$USER_PASSWORD
chrome-devtools-axi click @g1:3
chrome-devtools-axi wait "Dashboard"
chrome-devtools-axi fillform @g1:1=$USER_EMAIL @g1:2=$USER_PASSWORD
chrome-devtools-axi click @g1:3
chrome-devtools-axi wait "Dashboard"
Take baseline screenshot
截取基准截图
chrome-devtools-axi screenshot baseline.png
chrome-devtools-axi screenshot baseline.png
Navigate to feature
导航至功能页面
chrome-devtools-axi click @g2:10
chrome-devtools-axi wait "Feature Page"
chrome-devtools-axi click @g2:10
chrome-devtools-axi wait "Feature Page"
Interact with feature
与功能交互
chrome-devtools-axi fill @g3:5 "test data"
chrome-devtools-axi click @g3:6
chrome-devtools-axi fill @g3:5 "test data"
chrome-devtools-axi click @g3:6
Verify result
验证结果
chrome-devtools-axi wait "Success"
chrome-devtools-axi snapshot --full > result.txt
chrome-devtools-axi wait "Success"
chrome-devtools-axi snapshot --full > result.txt
Capture performance
捕获性能数据
chrome-devtools-axi lighthouse --output-dir ./reports
chrome-devtools-axi lighthouse --output-dir ./reports
Cleanup
清理会话
chrome-devtools-axi stop
undefinedchrome-devtools-axi stop
undefinedBest Practices for Agents
Agent最佳实践
- Always re-snapshot after page changes — Don't reuse refs across navigation or mutations
- Use combined operations — gives snapshot automatically, reducing round-trips
open - Check help hints — Every response includes suggested next steps
- Use sparingly — Default truncation saves tokens; only expand when needed
--full - Prefer over multiple
fillform— Batch form operations when possiblefill - Wait strategically — Use instead of arbitrary timeouts when possible
wait <text> - Leverage eval for extraction — JavaScript eval is more efficient than scraping via snapshots
- 页面更新后重新捕获快照 — 不要跨导航或页面变更复用引用
- 使用组合操作 — 命令会自动返回快照,减少往返请求
open - 查看帮助提示 — 每个响应都包含建议的下一步操作
- 谨慎使用参数 — 默认截断可节省令牌,仅在需要时展开完整内容
--full - 优先使用而非多次
fillform— 批量处理表单操作fill - 策略性等待 — 尽可能使用替代固定时长的超时等待
wait <text> - 利用执行命令提取数据 — JavaScript执行比快照提取更高效