Browser Automation
Automate browser interactions using the browse CLI with Claude.
Setup check
Before running any browser commands, verify the CLI is available:
CODEBLOCK0
Environment Selection (Local vs Remote)
The CLI automatically selects between local and remote browser environments based on available configuration:
Local mode (default)
- - Uses local Chrome — no API keys needed
- Best for: development, simple pages, trusted sites with no bot protection
Remote mode (Browserbase)
- - Activated when
BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID are set - Provides: anti-bot stealth, automatic CAPTCHA solving, residential proxies, session persistence
- Use remote mode when: the target site has bot detection, CAPTCHAs, IP rate limiting, Cloudflare protection, or requires geo-specific access
- Get credentials at https://browserbase.com/settings
When to choose which
- - Simple browsing (docs, wikis, public APIs): local mode is fine
- Protected sites (login walls, CAPTCHAs, anti-scraping): use remote mode
- If local mode fails with bot detection or access denied: switch to remote mode
Commands
All commands work identically in both modes. The daemon auto-starts on first command.
Navigation
CODEBLOCK1
Page state (prefer snapshot over screenshot)
CODEBLOCK2
Use browse snapshot as your default for understanding page state — it returns the accessibility tree with element refs you can use to interact. Only use browse screenshot when you need visual context (layout, images, debugging).
Interaction
CODEBLOCK3
Session management
CODEBLOCK4
Typical workflow
- 1.
browse open <url> — navigate to the page - INLINECODE5 — read the accessibility tree to understand page structure and get element refs
- INLINECODE6 /
browse type <text> / browse fill <selector> <value> — interact using refs from snapshot - INLINECODE9 — confirm the action worked
- Repeat 3-4 as needed
- INLINECODE10 — close the browser when done
Quick Example
CODEBLOCK5
Mode Comparison
| Feature | Local | Browserbase |
|---|
| Speed | Faster | Slightly slower |
| Setup |
Chrome required | API key required |
| Stealth mode | No | Yes (custom Chromium, anti-bot fingerprinting) |
| CAPTCHA solving | No | Yes (automatic reCAPTCHA/hCaptcha) |
| Residential proxies | No | Yes (201 countries, geo-targeting) |
| Session persistence | No | Yes (cookies/auth persist across sessions) |
| Best for | Development/simple pages | Protected sites, bot detection, production scraping |
Best Practices
- 1. Always
browse open first before interacting - Use
browse snapshot to check page state — it's fast and gives you element refs - Only screenshot when visual context is needed (layout checks, images, debugging)
- Use refs from snapshot to click/interact — e.g., INLINECODE13
browse stop when done to clean up the browser session
Troubleshooting
- - "No active page": Run
browse stop, then check browse status. If it still says running, kill the zombie daemon with pkill -f "browse.*daemon", then retry INLINECODE18 - Chrome not found: Install Chrome or use INLINECODE19
- Action fails: Run
browse snapshot to see available elements and their refs - Browserbase fails: Verify API key and project ID are set
Switching to Remote Mode
Switch to remote when you detect: CAPTCHAs (reCAPTCHA, hCaptcha, Turnstile), bot detection pages ("Checking your browser..."), HTTP 403/429, empty pages on sites that should have content, or the user asks for it.
Don't switch for simple sites (docs, wikis, public APIs, localhost).
CODEBLOCK6
The switch is sticky until you run browse stop or switch again.
For detailed examples, see EXAMPLES.md.
For API reference, see REFERENCE.md.
浏览器自动化
使用带有Claude的browse CLI自动化浏览器交互。
设置检查
在运行任何浏览器命令之前,验证CLI是否可用:
bash
which browse || npm install -g @browserbasehq/browse-cli
环境选择(本地 vs 远程)
CLI根据可用配置自动选择本地或远程浏览器环境:
本地模式(默认)
- - 使用本地Chrome — 无需API密钥
- 适用于:开发、简单页面、无机器人防护的可信站点
远程模式(Browserbase)
- - 当设置了BROWSERBASEAPIKEY和BROWSERBASEPROJECTID时激活
- 提供:反机器人隐身、自动验证码解决、住宅代理、会话持久化
- 在以下情况使用远程模式:目标站点有机器人检测、验证码、IP速率限制、Cloudflare防护,或需要特定地区访问
- 在https://browserbase.com/settings获取凭证
何时选择哪种模式
- - 简单浏览(文档、维基、公共API):本地模式即可
- 受保护站点(登录墙、验证码、反爬虫):使用远程模式
- 如果本地模式因机器人检测或访问被拒而失败:切换到远程模式
命令
所有命令在两种模式下工作方式相同。守护进程在首次命令时自动启动。
导航
bash
browse open
# 访问URL(别名:goto)
browse reload # 重新加载当前页面
browse back # 返回上一页
browse forward # 前进到下一页
页面状态(优先使用snapshot而非screenshot)
bash
browse snapshot # 获取带有元素引用的无障碍树(快速、结构化)
browse screenshot [path] # 截取视觉截图(慢速,消耗视觉令牌)
browse get url # 获取当前URL
browse get title # 获取页面标题
browse get text # 获取文本内容(使用body获取所有文本)
browse get html # 获取元素的HTML内容
browse get value # 获取表单字段值
默认使用browse snapshot来理解页面状态——它返回带有元素引用的无障碍树,可用于交互。仅在需要视觉上下文(布局、图像、调试)时使用browse screenshot。
交互
bash
browse click [ # 通过snapshot中的引用点击元素(例如@0-5)
browse type # 在聚焦元素中输入文本
browse fill # 填充输入框并按回车
browse select # 选择下拉选项
browse press # 按键(Enter、Tab、Escape、Cmd+A等)
browse drag # 从一点拖拽到另一点
browse scroll # 在坐标处滚动
browse highlight # 在页面上高亮元素
browse is visible # 检查元素是否可见
browse is checked # 检查元素是否被选中
browse wait [arg] # 等待:加载、选择器、超时
]会话管理
bash
browse stop # 停止浏览器守护进程
browse status # 检查守护进程状态(包括环境)
browse env # 显示当前环境(本地或远程)
browse env local # 切换到本地Chrome
browse env remote # 切换到Browserbase(需要API密钥)
browse pages # 列出所有打开的标签页
browse tab_switch # 按索引切换标签页
browse tab_close [index] # 关闭标签页
典型工作流程
- 1. browse open — 导航到页面
- browse snapshot — 读取无障碍树以了解页面结构并获取元素引用
- browse click
[ / browse type / browse fill — 使用snapshot中的引用进行交互] - browse snapshot — 确认操作生效
- 根据需要重复步骤3-4
- browse stop — 完成后关闭浏览器
快速示例
bash
browse open https://example.com
browse snapshot # 查看页面结构 + 元素引用
browse click @0-5 # 点击引用为0-5的元素
browse get title
browse stop
模式对比
需要Chrome | 需要API密钥 |
| 隐身模式 | 否 | 是(自定义Chromium,反机器人指纹识别) |
| 验证码解决 | 否 | 是(自动reCAPTCHA/hCaptcha) |
| 住宅代理 | 否 | 是(201个国家,地理定位) |
| 会话持久化 | 否 | 是(跨会话保持cookies/认证) |
| 最佳用途 | 开发/简单页面 | 受保护站点、机器人检测、生产环境爬取 |
最佳实践
- 1. 始终先执行browse open 再进行交互
- 使用browse snapshot 检查页面状态——快速且提供元素引用
- 仅在需要视觉上下文时截图(布局检查、图像、调试)
- 使用snapshot中的引用 进行点击/交互——例如browse click @0-5
- 完成后执行browse stop 清理浏览器会话
故障排除
- - 无活动页面:运行browse stop,然后检查browse status。如果仍显示运行中,使用pkill -f browse.*daemon杀死僵尸守护进程,然后重试browse open
- 未找到Chrome:安装Chrome或使用browse env remote
- 操作失败:运行browse snapshot查看可用元素及其引用
- Browserbase失败:验证API密钥和项目ID是否已设置
切换到远程模式
在检测到以下情况时切换到远程:验证码(reCAPTCHA、hCaptcha、Turnstile)、机器人检测页面(正在检查您的浏览器...)、HTTP 403/429、本应有内容的页面显示空白,或用户要求切换。
对于简单站点(文档、维基、公共API、localhost)不要切换。
bash
browse env remote # 切换到Browserbase
browse env local # 切换回本地Chrome
切换是持久的,直到运行browse stop或再次切换。
详细示例请参见EXAMPLES.md。
API参考请参见REFERENCE.md。