
Agent Browser
- 35 installs
- 760 repo stars
- Updated July 15, 2026
- countbot-ai/countbot
Automate websites from the CLI with agent-browser: navigate, snapshot element refs, fill forms, click, screenshot, and scrape data.
About
A browser-automation CLI for agents that navigates pages, snapshots interactive element refs, and drives clicks, form fills, and data extraction. A developer uses it when an agent needs to interact with or test web pages programmatically.
- Snapshot-then-interact workflow using element refs like @e1, @e2
- Re-snapshot after navigation or DOM changes for fresh refs
Agent Browser by the numbers
- 35 all-time installs (skills.sh)
- Ranked #1,180 of 2,715 Automation & Workflows skills by installs in the Skillselion catalog
- Data as of Jul 31, 2026 (Skillselion catalog sync)
npx skills add https://github.com/countbot-ai/countbot --skill agent-browserAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 35 |
|---|---|
| repo stars | ★ 760 |
| Last updated | July 15, 2026 |
| Repository | countbot-ai/countbot ↗ |
What it does
Automate websites from the CLI with agent-browser: navigate, snapshot element refs, fill forms, click, screenshot, and scrape data.
Files
Browser Automation with agent-browser
Core Workflow
Every browser automation follows this pattern:
1. Navigate: agent-browser open <url> 2. Snapshot: agent-browser snapshot -i (get element refs like @e1, @e2) 3. Interact: Use refs to click, fill, select 4. Re-snapshot: After navigation or DOM changes, get fresh refs
agent-browser open https://example.com/form
agent-browser snapshot -i
# Output: @e1 [input type="email"], @e2 [input type="password"], @e3 [button] "Submit"
agent-browser fill @e1 "user@example.com"
agent-browser fill @e2 "password123"
agent-browser click @e3
agent-browser wait --load networkidle
agent-browser snapshot -i # Check resultEssential Commands
# Navigation
agent-browser open <url> # Navigate (aliases: goto, navigate)
agent-browser close # Close browser
# Snapshot
agent-browser snapshot -i # Interactive elements with refs (recommended)
agent-browser snapshot -i -C # Include cursor-interactive elements (divs with onclick, cursor:pointer)
agent-browser snapshot -s "#selector" # Scope to CSS selector
# Interaction (use @refs from snapshot)
agent-browser click @e1 # Click element
agent-browser fill @e2 "text" # Clear and type text
agent-browser type @e2 "text" # Type without clearing
agent-browser select @e1 "option" # Select dropdown option
agent-browser check @e1 # Check checkbox
agent-browser press Enter # Press key
agent-browser scroll down 500 # Scroll page
# Get information
agent-browser get text @e1 # Get element text
agent-browser get url # Get current URL
agent-browser get title # Get page title
# Wait
agent-browser wait @e1 # Wait for element
agent-browser wait --load networkidle # Wait for network idle
agent-browser wait --url "**/page" # Wait for URL pattern
agent-browser wait 2000 # Wait milliseconds
# Capture
agent-browser screenshot # Screenshot to temp dir
agent-browser screenshot --full # Full page screenshot
agent-browser pdf output.pdf # Save as PDFCommon Patterns
Form Submission
agent-browser open https://example.com/signup
agent-browser snapshot -i
agent-browser fill @e1 "Jane Doe"
agent-browser fill @e2 "jane@example.com"
agent-browser select @e3 "California"
agent-browser check @e4
agent-browser click @e5
agent-browser wait --load networkidleAuthentication with State Persistence
# Login once and save state
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
# Reuse in future sessions
agent-browser state load auth.json
agent-browser open https://app.example.com/dashboardSession Persistence
# Auto-save/restore cookies and localStorage across browser restarts
agent-browser --session-name myapp open https://app.example.com/login
# ... login flow ...
agent-browser close # State auto-saved to ~/.agent-browser/sessions/
# Next time, state is auto-loaded
agent-browser --session-name myapp open https://app.example.com/dashboard
# Encrypt state at rest
export AGENT_BROWSER_ENCRYPTION_KEY=$(openssl rand -hex 32)
agent-browser --session-name secure open https://app.example.com
# Manage saved states
agent-browser state list
agent-browser state show myapp-default.json
agent-browser state clear myapp
agent-browser state clean --older-than 7Data Extraction
agent-browser open https://example.com/products
agent-browser snapshot -i
agent-browser get text @e5 # Get specific element text
agent-browser get text body > page.txt # Get all page text
# JSON output for parsing
agent-browser snapshot -i --json
agent-browser get text @e1 --jsonParallel Sessions
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 listConnect to Existing Chrome
# Auto-discover running Chrome with remote debugging enabled
agent-browser --auto-connect open https://example.com
agent-browser --auto-connect snapshot
# Or with explicit CDP port
agent-browser --cdp 9222 snapshotVisual Browser (Debugging)
agent-browser --headed open https://example.com
agent-browser highlight @e1 # Highlight element
agent-browser record start demo.webm # Record sessionLocal Files (PDFs, HTML)
# Open local files with file:// URLs
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.pngiOS Simulator (Mobile Safari)
# List available iOS simulators
agent-browser device list
# Launch Safari on a specific device
agent-browser -p ios --device "iPhone 16 Pro" open https://example.com
# Same workflow as desktop - snapshot, interact, re-snapshot
agent-browser -p ios snapshot -i
agent-browser -p ios tap @e1 # Tap (alias for click)
agent-browser -p ios fill @e2 "text"
agent-browser -p ios swipe up # Mobile-specific gesture
# Take screenshot
agent-browser -p ios screenshot mobile.png
# Close session (shuts down simulator)
agent-browser -p ios closeRequirements: macOS with Xcode, Appium (npm install -g appium && appium driver install xcuitest)
Real devices: Works with physical iOS devices if pre-configured. Use --device "<UDID>" where UDID is from xcrun xctrace list devices.
Timeouts and Slow Pages
The default Playwright timeout is 60 seconds for local browsers. For slow websites or large pages, use explicit waits instead of relying on the default timeout:
# Wait for network activity to settle (best for slow pages)
agent-browser wait --load networkidle
# Wait for a specific element to appear
agent-browser wait "#content"
agent-browser wait @e1
# Wait for a specific URL pattern (useful after redirects)
agent-browser wait --url "**/dashboard"
# Wait for a JavaScript condition
agent-browser wait --fn "document.readyState === 'complete'"
# Wait a fixed duration (milliseconds) as a last resort
agent-browser wait 5000When dealing with consistently slow websites, use wait --load networkidle after open to ensure the page is fully loaded before taking a snapshot. If a specific element is slow to render, wait for it directly with wait <selector> or wait @ref.
Session Management and Cleanup
When running multiple agents or automations concurrently, always use named sessions to avoid conflicts:
# Each agent gets its own isolated session
agent-browser --session agent1 open site-a.com
agent-browser --session agent2 open site-b.com
# Check active sessions
agent-browser session listAlways close your browser session when done to avoid leaked processes:
agent-browser close # Close default session
agent-browser --session agent1 close # Close specific sessionIf a previous session was not closed properly, the daemon may still be running. Use agent-browser close to clean it up before starting new work.
Ref Lifecycle (Important)
Refs (@e1, @e2, etc.) are invalidated when the page changes. Always re-snapshot after:
- Clicking links or buttons that navigate
- Form submissions
- Dynamic content loading (dropdowns, modals)
agent-browser click @e5 # Navigates to new page
agent-browser snapshot -i # MUST re-snapshot
agent-browser click @e1 # Use new refsSemantic Locators (Alternative to Refs)
When refs are unavailable or unreliable, use semantic locators:
agent-browser find text "Sign In" click
agent-browser find label "Email" fill "user@test.com"
agent-browser find role button click --name "Submit"
agent-browser find placeholder "Search" type "query"
agent-browser find testid "submit-btn" clickJavaScript Evaluation (eval)
Use eval to run JavaScript in the browser context. Shell quoting can corrupt complex expressions -- use --stdin or -b to avoid issues.
# Simple expressions work with regular quoting
agent-browser eval 'document.title'
agent-browser eval 'document.querySelectorAll("img").length'
# Complex JS: use --stdin with heredoc (RECOMMENDED)
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
# Alternative: base64 encoding (avoids all shell escaping issues)
agent-browser eval -b "$(echo -n 'Array.from(document.querySelectorAll("a")).map(a => a.href)' | base64)"Why this matters: When the shell processes your command, inner double quotes, ! characters (history expansion), backticks, and $() can all corrupt the JavaScript before it reaches agent-browser. The --stdin and -b flags bypass shell interpretation entirely.
Rules of thumb:
- Single-line, no nested quotes -> regular
eval 'expression'with single quotes is fine - Nested quotes, arrow functions, template literals, or multiline -> use
eval --stdin <<'EVALEOF' - Programmatic/generated scripts -> use
eval -bwith base64
Deep-Dive Documentation
| Reference | When to Use |
|---|---|
| references/commands.md | Full command reference with all options |
| references/snapshot-refs.md | Ref lifecycle, invalidation rules, troubleshooting |
| references/session-management.md | Parallel sessions, state persistence, concurrent scraping |
| references/authentication.md | Login flows, OAuth, 2FA handling, state reuse |
| references/video-recording.md | Recording workflows for debugging and documentation |
| references/proxy-support.md | Proxy configuration, geo-testing, rotating proxies |
Ready-to-Use Templates
| Template | Description |
|---|---|
| templates/form-automation.sh | Form filling with validation |
| templates/authenticated-session.sh | Login once, reuse state |
| templates/capture-workflow.sh | Content extraction with screenshots |
./templates/form-automation.sh https://example.com/form
./templates/authenticated-session.sh https://app.example.com/login
./templates/capture-workflow.sh https://example.com ./outputAuthentication Patterns
Login flows, session persistence, OAuth, 2FA, and authenticated browsing.
Related: session-management.md for state persistence details, SKILL.md for quick start.
Contents
- Basic Login Flow
- Saving Authentication State
- Restoring Authentication
- OAuth / SSO Flows
- Two-Factor Authentication
- HTTP Basic Auth
- Cookie-Based Auth
- Token Refresh Handling
- Security Best Practices
Basic Login Flow
# Navigate to login page
agent-browser open https://app.example.com/login
agent-browser wait --load networkidle
# Get form elements
agent-browser snapshot -i
# Output: @e1 [input type="email"], @e2 [input type="password"], @e3 [button] "Sign In"
# Fill credentials
agent-browser fill @e1 "user@example.com"
agent-browser fill @e2 "password123"
# Submit
agent-browser click @e3
agent-browser wait --load networkidle
# Verify login succeeded
agent-browser get url # Should be dashboard, not loginSaving Authentication State
After logging in, save state for reuse:
# Login first (see above)
agent-browser open https://app.example.com/login
agent-browser snapshot -i
agent-browser fill @e1 "user@example.com"
agent-browser fill @e2 "password123"
agent-browser click @e3
agent-browser wait --url "**/dashboard"
# Save authenticated state
agent-browser state save ./auth-state.jsonRestoring Authentication
Skip login by loading saved state:
# Load saved auth state
agent-browser state load ./auth-state.json
# Navigate directly to protected page
agent-browser open https://app.example.com/dashboard
# Verify authenticated
agent-browser snapshot -iOAuth / SSO Flows
For OAuth redirects:
# Start OAuth flow
agent-browser open https://app.example.com/auth/google
# Handle redirects automatically
agent-browser wait --url "**/accounts.google.com**"
agent-browser snapshot -i
# Fill Google credentials
agent-browser fill @e1 "user@gmail.com"
agent-browser click @e2 # Next button
agent-browser wait 2000
agent-browser snapshot -i
agent-browser fill @e3 "password"
agent-browser click @e4 # Sign in
# Wait for redirect back
agent-browser wait --url "**/app.example.com**"
agent-browser state save ./oauth-state.jsonTwo-Factor Authentication
Handle 2FA with manual intervention:
# Login with credentials
agent-browser open https://app.example.com/login --headed # Show browser
agent-browser snapshot -i
agent-browser fill @e1 "user@example.com"
agent-browser fill @e2 "password123"
agent-browser click @e3
# Wait for user to complete 2FA manually
echo "Complete 2FA in the browser window..."
agent-browser wait --url "**/dashboard" --timeout 120000
# Save state after 2FA
agent-browser state save ./2fa-state.jsonHTTP Basic Auth
For sites using HTTP Basic Authentication:
# Set credentials before navigation
agent-browser set credentials username password
# Navigate to protected resource
agent-browser open https://protected.example.com/apiCookie-Based Auth
Manually set authentication cookies:
# Set auth cookie
agent-browser cookies set session_token "abc123xyz"
# Navigate to protected page
agent-browser open https://app.example.com/dashboardToken Refresh Handling
For sessions with expiring tokens:
#!/bin/bash
# Wrapper that handles token refresh
STATE_FILE="./auth-state.json"
# Try loading existing state
if [[ -f "$STATE_FILE" ]]; then
agent-browser state load "$STATE_FILE"
agent-browser open https://app.example.com/dashboard
# Check if session is still valid
URL=$(agent-browser get url)
if [[ "$URL" == *"/login"* ]]; then
echo "Session expired, re-authenticating..."
# Perform fresh 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 "$STATE_FILE"
fi
else
# First-time login
agent-browser open https://app.example.com/login
# ... login flow ...
fiSecurity Best Practices
1. Never commit state files - They contain session tokens
echo "*.auth-state.json" >> .gitignore2. Use environment variables for credentials
agent-browser fill @e1 "$APP_USERNAME"
agent-browser fill @e2 "$APP_PASSWORD"3. Clean up after automation
agent-browser cookies clear
rm -f ./auth-state.json4. Use short-lived sessions for CI/CD
# Don't persist state in CI
agent-browser open https://app.example.com/login
# ... login and perform actions ...
agent-browser close # Session ends, nothing persistedCommand Reference
Complete reference for all agent-browser commands. For quick start and common patterns, see SKILL.md.
Navigation
agent-browser open <url> # Navigate to URL (aliases: goto, navigate)
# Supports: https://, http://, file://, about:, data://
# Auto-prepends https:// if no protocol given
agent-browser back # Go back
agent-browser forward # Go forward
agent-browser reload # Reload page
agent-browser close # Close browser (aliases: quit, exit)
agent-browser connect 9222 # Connect to browser via CDP portSnapshot (page analysis)
agent-browser snapshot # Full accessibility tree
agent-browser snapshot -i # Interactive elements only (recommended)
agent-browser snapshot -c # Compact output
agent-browser snapshot -d 3 # Limit depth to 3
agent-browser snapshot -s "#main" # Scope to CSS selectorInteractions (use @refs from snapshot)
agent-browser click @e1 # Click
agent-browser dblclick @e1 # Double-click
agent-browser focus @e1 # Focus element
agent-browser fill @e2 "text" # Clear and type
agent-browser type @e2 "text" # Type without clearing
agent-browser press Enter # Press key (alias: key)
agent-browser press Control+a # Key combination
agent-browser keydown Shift # Hold key down
agent-browser keyup Shift # Release key
agent-browser hover @e1 # Hover
agent-browser check @e1 # Check checkbox
agent-browser uncheck @e1 # Uncheck checkbox
agent-browser select @e1 "value" # Select dropdown option
agent-browser select @e1 "a" "b" # Select multiple options
agent-browser scroll down 500 # Scroll page (default: down 300px)
agent-browser scrollintoview @e1 # Scroll element into view (alias: scrollinto)
agent-browser drag @e1 @e2 # Drag and drop
agent-browser upload @e1 file.pdf # Upload filesGet Information
agent-browser get text @e1 # Get element text
agent-browser get html @e1 # Get innerHTML
agent-browser get value @e1 # Get input value
agent-browser get attr @e1 href # Get attribute
agent-browser get title # Get page title
agent-browser get url # Get current URL
agent-browser get count ".item" # Count matching elements
agent-browser get box @e1 # Get bounding box
agent-browser get styles @e1 # Get computed styles (font, color, bg, etc.)Check State
agent-browser is visible @e1 # Check if visible
agent-browser is enabled @e1 # Check if enabled
agent-browser is checked @e1 # Check if checkedScreenshots and PDF
agent-browser screenshot # Save to temporary directory
agent-browser screenshot path.png # Save to specific path
agent-browser screenshot --full # Full page
agent-browser pdf output.pdf # Save as PDFVideo Recording
agent-browser record start ./demo.webm # Start recording
agent-browser click @e1 # Perform actions
agent-browser record stop # Stop and save video
agent-browser record restart ./take2.webm # Stop current + start newWait
agent-browser wait @e1 # Wait for element
agent-browser wait 2000 # Wait milliseconds
agent-browser wait --text "Success" # Wait for text (or -t)
agent-browser wait --url "**/dashboard" # Wait for URL pattern (or -u)
agent-browser wait --load networkidle # Wait for network idle (or -l)
agent-browser wait --fn "window.ready" # Wait for JS condition (or -f)Mouse Control
agent-browser mouse move 100 200 # Move mouse
agent-browser mouse down left # Press button
agent-browser mouse up left # Release button
agent-browser mouse wheel 100 # Scroll wheelSemantic Locators (alternative to refs)
agent-browser find role button click --name "Submit"
agent-browser find text "Sign In" click
agent-browser find text "Sign In" click --exact # Exact match only
agent-browser find label "Email" fill "user@test.com"
agent-browser find placeholder "Search" type "query"
agent-browser find alt "Logo" click
agent-browser find title "Close" click
agent-browser find testid "submit-btn" click
agent-browser find first ".item" click
agent-browser find last ".item" click
agent-browser find nth 2 "a" hoverBrowser Settings
agent-browser set viewport 1920 1080 # Set viewport size
agent-browser set device "iPhone 14" # Emulate device
agent-browser set geo 37.7749 -122.4194 # Set geolocation (alias: geolocation)
agent-browser set offline on # Toggle offline mode
agent-browser set headers '{"X-Key":"v"}' # Extra HTTP headers
agent-browser set credentials user pass # HTTP basic auth (alias: auth)
agent-browser set media dark # Emulate color scheme
agent-browser set media light reduced-motion # Light mode + reduced motionCookies and Storage
agent-browser cookies # Get all cookies
agent-browser cookies set name value # Set cookie
agent-browser cookies clear # Clear cookies
agent-browser storage local # Get all localStorage
agent-browser storage local key # Get specific key
agent-browser storage local set k v # Set value
agent-browser storage local clear # Clear allNetwork
agent-browser network route <url> # Intercept requests
agent-browser network route <url> --abort # Block requests
agent-browser network route <url> --body '{}' # Mock response
agent-browser network unroute [url] # Remove routes
agent-browser network requests # View tracked requests
agent-browser network requests --filter api # Filter requestsTabs and Windows
agent-browser tab # List tabs
agent-browser tab new [url] # New tab
agent-browser tab 2 # Switch to tab by index
agent-browser tab close # Close current tab
agent-browser tab close 2 # Close tab by index
agent-browser window new # New windowFrames
agent-browser frame "#iframe" # Switch to iframe
agent-browser frame main # Back to main frameDialogs
agent-browser dialog accept [text] # Accept dialog
agent-browser dialog dismiss # Dismiss dialogJavaScript
agent-browser eval "document.title" # Simple expressions only
agent-browser eval -b "<base64>" # Any JavaScript (base64 encoded)
agent-browser eval --stdin # Read script from stdinUse -b/--base64 or --stdin for reliable execution. Shell escaping with nested quotes and special characters is error-prone.
# Base64 encode your script, then:
agent-browser eval -b "ZG9jdW1lbnQucXVlcnlTZWxlY3RvcignW3NyYyo9Il9uZXh0Il0nKQ=="
# Or use stdin with heredoc for multiline scripts:
cat <<'EOF' | agent-browser eval --stdin
const links = document.querySelectorAll('a');
Array.from(links).map(a => a.href);
EOFState Management
agent-browser state save auth.json # Save cookies, storage, auth state
agent-browser state load auth.json # Restore saved stateGlobal Options
agent-browser --session <name> ... # Isolated browser session
agent-browser --json ... # JSON output for parsing
agent-browser --headed ... # Show browser window (not headless)
agent-browser --full ... # Full page screenshot (-f)
agent-browser --cdp <port> ... # Connect via Chrome DevTools Protocol
agent-browser -p <provider> ... # Cloud browser provider (--provider)
agent-browser --proxy <url> ... # Use proxy server
agent-browser --headers <json> ... # HTTP headers scoped to URL's origin
agent-browser --executable-path <p> # Custom browser executable
agent-browser --extension <path> ... # Load browser extension (repeatable)
agent-browser --ignore-https-errors # Ignore SSL certificate errors
agent-browser --help # Show help (-h)
agent-browser --version # Show version (-V)
agent-browser <command> --help # Show detailed help for a commandDebugging
agent-browser --headed open example.com # Show browser window
agent-browser --cdp 9222 snapshot # Connect via CDP port
agent-browser connect 9222 # Alternative: connect command
agent-browser console # View console messages
agent-browser console --clear # Clear console
agent-browser errors # View page errors
agent-browser errors --clear # Clear errors
agent-browser highlight @e1 # Highlight element
agent-browser trace start # Start recording trace
agent-browser trace stop trace.zip # Stop and save traceEnvironment Variables
AGENT_BROWSER_SESSION="mysession" # Default session name
AGENT_BROWSER_EXECUTABLE_PATH="/path/chrome" # Custom browser path
AGENT_BROWSER_EXTENSIONS="/ext1,/ext2" # Comma-separated extension paths
AGENT_BROWSER_PROVIDER="browserbase" # Cloud browser provider
AGENT_BROWSER_STREAM_PORT="9223" # WebSocket streaming port
AGENT_BROWSER_HOME="/path/to/agent-browser" # Custom install locationProxy Support
Proxy configuration for geo-testing, rate limiting avoidance, and corporate environments.
Related: commands.md for global options, SKILL.md for quick start.
Contents
- Basic Proxy Configuration
- Authenticated Proxy
- SOCKS Proxy
- Proxy Bypass
- Common Use Cases
- Verifying Proxy Connection
- Troubleshooting
- Best Practices
Basic Proxy Configuration
Set proxy via environment variable before starting:
# HTTP proxy
export HTTP_PROXY="http://proxy.example.com:8080"
agent-browser open https://example.com
# HTTPS proxy
export HTTPS_PROXY="https://proxy.example.com:8080"
agent-browser open https://example.com
# Both
export HTTP_PROXY="http://proxy.example.com:8080"
export HTTPS_PROXY="http://proxy.example.com:8080"
agent-browser open https://example.comAuthenticated Proxy
For proxies requiring authentication:
# Include credentials in URL
export HTTP_PROXY="http://username:password@proxy.example.com:8080"
agent-browser open https://example.comSOCKS Proxy
# SOCKS5 proxy
export ALL_PROXY="socks5://proxy.example.com:1080"
agent-browser open https://example.com
# SOCKS5 with auth
export ALL_PROXY="socks5://user:pass@proxy.example.com:1080"
agent-browser open https://example.comProxy Bypass
Skip proxy for specific domains:
# Bypass proxy for local addresses
export NO_PROXY="localhost,127.0.0.1,.internal.company.com"
agent-browser open https://internal.company.com # Direct connection
agent-browser open https://external.com # Via proxyCommon Use Cases
Geo-Location Testing
#!/bin/bash
# Test site from different regions using geo-located proxies
PROXIES=(
"http://us-proxy.example.com:8080"
"http://eu-proxy.example.com:8080"
"http://asia-proxy.example.com:8080"
)
for proxy in "${PROXIES[@]}"; do
export HTTP_PROXY="$proxy"
export HTTPS_PROXY="$proxy"
region=$(echo "$proxy" | grep -oP '^\w+-\w+')
echo "Testing from: $region"
agent-browser --session "$region" open https://example.com
agent-browser --session "$region" screenshot "./screenshots/$region.png"
agent-browser --session "$region" close
doneRotating Proxies for Scraping
#!/bin/bash
# Rotate through proxy list to avoid rate limiting
PROXY_LIST=(
"http://proxy1.example.com:8080"
"http://proxy2.example.com:8080"
"http://proxy3.example.com:8080"
)
URLS=(
"https://site.com/page1"
"https://site.com/page2"
"https://site.com/page3"
)
for i in "${!URLS[@]}"; do
proxy_index=$((i % ${#PROXY_LIST[@]}))
export HTTP_PROXY="${PROXY_LIST[$proxy_index]}"
export HTTPS_PROXY="${PROXY_LIST[$proxy_index]}"
agent-browser open "${URLS[$i]}"
agent-browser get text body > "output-$i.txt"
agent-browser close
sleep 1 # Polite delay
doneCorporate Network Access
#!/bin/bash
# Access internal sites via corporate proxy
export HTTP_PROXY="http://corpproxy.company.com:8080"
export HTTPS_PROXY="http://corpproxy.company.com:8080"
export NO_PROXY="localhost,127.0.0.1,.company.com"
# External sites go through proxy
agent-browser open https://external-vendor.com
# Internal sites bypass proxy
agent-browser open https://intranet.company.comVerifying Proxy Connection
# Check your apparent IP
agent-browser open https://httpbin.org/ip
agent-browser get text body
# Should show proxy's IP, not your real IPTroubleshooting
Proxy Connection Failed
# Test proxy connectivity first
curl -x http://proxy.example.com:8080 https://httpbin.org/ip
# Check if proxy requires auth
export HTTP_PROXY="http://user:pass@proxy.example.com:8080"SSL/TLS Errors Through Proxy
Some proxies perform SSL inspection. If you encounter certificate errors:
# For testing only - not recommended for production
agent-browser open https://example.com --ignore-https-errorsSlow Performance
# Use proxy only when necessary
export NO_PROXY="*.cdn.com,*.static.com" # Direct CDN accessBest Practices
1. Use environment variables - Don't hardcode proxy credentials 2. Set NO_PROXY appropriately - Avoid routing local traffic through proxy 3. Test proxy before automation - Verify connectivity with simple requests 4. Handle proxy failures gracefully - Implement retry logic for unstable proxies 5. Rotate proxies for large scraping jobs - Distribute load and avoid bans
Session Management
Multiple isolated browser sessions with state persistence and concurrent browsing.
Related: authentication.md for login patterns, SKILL.md for quick start.
Contents
- Named Sessions
- Session Isolation Properties
- Session State Persistence
- Common Patterns
- Default Session
- Session Cleanup
- Best Practices
Named Sessions
Use --session flag to isolate browser contexts:
# Session 1: Authentication flow
agent-browser --session auth open https://app.example.com/login
# Session 2: Public browsing (separate cookies, storage)
agent-browser --session public open https://example.com
# Commands are isolated by session
agent-browser --session auth fill @e1 "user@example.com"
agent-browser --session public get text bodySession Isolation Properties
Each session has independent:
- Cookies
- LocalStorage / SessionStorage
- IndexedDB
- Cache
- Browsing history
- Open tabs
Session State Persistence
Save Session State
# Save cookies, storage, and auth state
agent-browser state save /path/to/auth-state.jsonLoad Session State
# Restore saved state
agent-browser state load /path/to/auth-state.json
# Continue with authenticated session
agent-browser open https://app.example.com/dashboardState File Contents
{
"cookies": [...],
"localStorage": {...},
"sessionStorage": {...},
"origins": [...]
}Common Patterns
Authenticated Session Reuse
#!/bin/bash
# Save login state once, reuse many times
STATE_FILE="/tmp/auth-state.json"
# Check if we have saved state
if [[ -f "$STATE_FILE" ]]; then
agent-browser state load "$STATE_FILE"
agent-browser open https://app.example.com/dashboard
else
# Perform login
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 --load networkidle
# Save for future use
agent-browser state save "$STATE_FILE"
fiConcurrent Scraping
#!/bin/bash
# Scrape multiple sites concurrently
# Start all sessions
agent-browser --session site1 open https://site1.com &
agent-browser --session site2 open https://site2.com &
agent-browser --session site3 open https://site3.com &
wait
# Extract from each
agent-browser --session site1 get text body > site1.txt
agent-browser --session site2 get text body > site2.txt
agent-browser --session site3 get text body > site3.txt
# Cleanup
agent-browser --session site1 close
agent-browser --session site2 close
agent-browser --session site3 closeA/B Testing Sessions
# Test different user experiences
agent-browser --session variant-a open "https://app.com?variant=a"
agent-browser --session variant-b open "https://app.com?variant=b"
# Compare
agent-browser --session variant-a screenshot /tmp/variant-a.png
agent-browser --session variant-b screenshot /tmp/variant-b.pngDefault Session
When --session is omitted, commands use the default session:
# These use the same default session
agent-browser open https://example.com
agent-browser snapshot -i
agent-browser close # Closes default sessionSession Cleanup
# Close specific session
agent-browser --session auth close
# List active sessions
agent-browser session listBest Practices
1. Name Sessions Semantically
# GOOD: Clear purpose
agent-browser --session github-auth open https://github.com
agent-browser --session docs-scrape open https://docs.example.com
# AVOID: Generic names
agent-browser --session s1 open https://github.com2. Always Clean Up
# Close sessions when done
agent-browser --session auth close
agent-browser --session scrape close3. Handle State Files Securely
# Don't commit state files (contain auth tokens!)
echo "*.auth-state.json" >> .gitignore
# Delete after use
rm /tmp/auth-state.json4. Timeout Long Sessions
# Set timeout for automated scripts
timeout 60 agent-browser --session long-task get text bodySnapshot and Refs
Compact element references that reduce context usage dramatically for AI agents.
Related: commands.md for full command reference, SKILL.md for quick start.
Contents
- How Refs Work
- Snapshot Command
- Using Refs
- Ref Lifecycle
- Best Practices
- Ref Notation Details
- Troubleshooting
How Refs Work
Traditional approach:
Full DOM/HTML → AI parses → CSS selector → Action (~3000-5000 tokens)agent-browser approach:
Compact snapshot → @refs assigned → Direct interaction (~200-400 tokens)The Snapshot Command
# Basic snapshot (shows page structure)
agent-browser snapshot
# Interactive snapshot (-i flag) - RECOMMENDED
agent-browser snapshot -iSnapshot Output Format
Page: Example Site - Home
URL: https://example.com
@e1 [header]
@e2 [nav]
@e3 [a] "Home"
@e4 [a] "Products"
@e5 [a] "About"
@e6 [button] "Sign In"
@e7 [main]
@e8 [h1] "Welcome"
@e9 [form]
@e10 [input type="email"] placeholder="Email"
@e11 [input type="password"] placeholder="Password"
@e12 [button type="submit"] "Log In"
@e13 [footer]
@e14 [a] "Privacy Policy"Using Refs
Once you have refs, interact directly:
# Click the "Sign In" button
agent-browser click @e6
# Fill email input
agent-browser fill @e10 "user@example.com"
# Fill password
agent-browser fill @e11 "password123"
# Submit the form
agent-browser click @e12Ref Lifecycle
IMPORTANT: Refs are invalidated when the page changes!
# Get initial snapshot
agent-browser snapshot -i
# @e1 [button] "Next"
# Click triggers page change
agent-browser click @e1
# MUST re-snapshot to get new refs!
agent-browser snapshot -i
# @e1 [h1] "Page 2" ← Different element now!Best Practices
1. Always Snapshot Before Interacting
# CORRECT
agent-browser open https://example.com
agent-browser snapshot -i # Get refs first
agent-browser click @e1 # Use ref
# WRONG
agent-browser open https://example.com
agent-browser click @e1 # Ref doesn't exist yet!2. Re-Snapshot After Navigation
agent-browser click @e5 # Navigates to new page
agent-browser snapshot -i # Get new refs
agent-browser click @e1 # Use new refs3. Re-Snapshot After Dynamic Changes
agent-browser click @e1 # Opens dropdown
agent-browser snapshot -i # See dropdown items
agent-browser click @e7 # Select item4. Snapshot Specific Regions
For complex pages, snapshot specific areas:
# Snapshot just the form
agent-browser snapshot @e9Ref Notation Details
@e1 [tag type="value"] "text content" placeholder="hint"
│ │ │ │ │
│ │ │ │ └─ Additional attributes
│ │ │ └─ Visible text
│ │ └─ Key attributes shown
│ └─ HTML tag name
└─ Unique ref IDCommon Patterns
@e1 [button] "Submit" # Button with text
@e2 [input type="email"] # Email input
@e3 [input type="password"] # Password input
@e4 [a href="/page"] "Link Text" # Anchor link
@e5 [select] # Dropdown
@e6 [textarea] placeholder="Message" # Text area
@e7 [div class="modal"] # Container (when relevant)
@e8 [img alt="Logo"] # Image
@e9 [checkbox] checked # Checked checkbox
@e10 [radio] selected # Selected radioTroubleshooting
"Ref not found" Error
# Ref may have changed - re-snapshot
agent-browser snapshot -iElement Not Visible in Snapshot
# Scroll to reveal element
agent-browser scroll --bottom
agent-browser snapshot -i
# Or wait for dynamic content
agent-browser wait 1000
agent-browser snapshot -iToo Many Elements
# Snapshot specific container
agent-browser snapshot @e5
# Or use get text for content-only extraction
agent-browser get text @e5Video Recording
Capture browser automation as video for debugging, documentation, or verification.
Related: commands.md for full command reference, SKILL.md for quick start.
Contents
Basic Recording
# Start recording
agent-browser record start ./demo.webm
# Perform actions
agent-browser open https://example.com
agent-browser snapshot -i
agent-browser click @e1
agent-browser fill @e2 "test input"
# Stop and save
agent-browser record stopRecording Commands
# Start recording to file
agent-browser record start ./output.webm
# Stop current recording
agent-browser record stop
# Restart with new file (stops current + starts new)
agent-browser record restart ./take2.webmUse Cases
Debugging Failed Automation
#!/bin/bash
# Record automation for debugging
agent-browser record start ./debug-$(date +%Y%m%d-%H%M%S).webm
# Run your automation
agent-browser open https://app.example.com
agent-browser snapshot -i
agent-browser click @e1 || {
echo "Click failed - check recording"
agent-browser record stop
exit 1
}
agent-browser record stopDocumentation Generation
#!/bin/bash
# Record workflow for documentation
agent-browser record start ./docs/how-to-login.webm
agent-browser open https://app.example.com/login
agent-browser wait 1000 # Pause for visibility
agent-browser snapshot -i
agent-browser fill @e1 "demo@example.com"
agent-browser wait 500
agent-browser fill @e2 "password"
agent-browser wait 500
agent-browser click @e3
agent-browser wait --load networkidle
agent-browser wait 1000 # Show result
agent-browser record stopCI/CD Test Evidence
#!/bin/bash
# Record E2E test runs for CI artifacts
TEST_NAME="${1:-e2e-test}"
RECORDING_DIR="./test-recordings"
mkdir -p "$RECORDING_DIR"
agent-browser record start "$RECORDING_DIR/$TEST_NAME-$(date +%s).webm"
# Run test
if run_e2e_test; then
echo "Test passed"
else
echo "Test failed - recording saved"
fi
agent-browser record stopBest Practices
1. Add Pauses for Clarity
# Slow down for human viewing
agent-browser click @e1
agent-browser wait 500 # Let viewer see result2. Use Descriptive Filenames
# Include context in filename
agent-browser record start ./recordings/login-flow-2024-01-15.webm
agent-browser record start ./recordings/checkout-test-run-42.webm3. Handle Recording in Error Cases
#!/bin/bash
set -e
cleanup() {
agent-browser record stop 2>/dev/null || true
agent-browser close 2>/dev/null || true
}
trap cleanup EXIT
agent-browser record start ./automation.webm
# ... automation steps ...4. Combine with Screenshots
# Record video AND capture key frames
agent-browser record start ./flow.webm
agent-browser open https://example.com
agent-browser screenshot ./screenshots/step1-homepage.png
agent-browser click @e1
agent-browser screenshot ./screenshots/step2-after-click.png
agent-browser record stopOutput Format
- Default format: WebM (VP8/VP9 codec)
- Compatible with all modern browsers and video players
- Compressed but high quality
Limitations
- Recording adds slight overhead to automation
- Large recordings can consume significant disk space
- Some headless environments may have codec limitations
#!/bin/bash
# Template: Authenticated Session Workflow
# Purpose: Login once, save state, reuse for subsequent runs
# Usage: ./authenticated-session.sh <login-url> [state-file]
#
# Environment variables:
# APP_USERNAME - Login username/email
# APP_PASSWORD - Login password
#
# Two modes:
# 1. Discovery mode (default): Shows form structure so you can identify refs
# 2. Login mode: Performs actual login after you update the refs
#
# Setup steps:
# 1. Run once to see form structure (discovery mode)
# 2. Update refs in LOGIN FLOW section below
# 3. Set APP_USERNAME and APP_PASSWORD
# 4. Delete the DISCOVERY section
set -euo pipefail
LOGIN_URL="${1:?Usage: $0 <login-url> [state-file]}"
STATE_FILE="${2:-./auth-state.json}"
echo "Authentication workflow: $LOGIN_URL"
# ================================================================
# SAVED STATE: Skip login if valid saved state exists
# ================================================================
if [[ -f "$STATE_FILE" ]]; then
echo "Loading saved state from $STATE_FILE..."
if agent-browser --state "$STATE_FILE" open "$LOGIN_URL" 2>/dev/null; then
agent-browser wait --load networkidle
CURRENT_URL=$(agent-browser get url)
if [[ "$CURRENT_URL" != *"login"* ]] && [[ "$CURRENT_URL" != *"signin"* ]]; then
echo "Session restored successfully"
agent-browser snapshot -i
exit 0
fi
echo "Session expired, performing fresh login..."
agent-browser close 2>/dev/null || true
else
echo "Failed to load state, re-authenticating..."
fi
rm -f "$STATE_FILE"
fi
# ================================================================
# DISCOVERY MODE: Shows form structure (delete after setup)
# ================================================================
echo "Opening login page..."
agent-browser open "$LOGIN_URL"
agent-browser wait --load networkidle
echo ""
echo "Login form structure:"
echo "---"
agent-browser snapshot -i
echo "---"
echo ""
echo "Next steps:"
echo " 1. Note the refs: username=@e?, password=@e?, submit=@e?"
echo " 2. Update the LOGIN FLOW section below with your refs"
echo " 3. Set: export APP_USERNAME='...' APP_PASSWORD='...'"
echo " 4. Delete this DISCOVERY MODE section"
echo ""
agent-browser close
exit 0
# ================================================================
# LOGIN FLOW: Uncomment and customize after discovery
# ================================================================
# : "${APP_USERNAME:?Set APP_USERNAME environment variable}"
# : "${APP_PASSWORD:?Set APP_PASSWORD environment variable}"
#
# agent-browser open "$LOGIN_URL"
# agent-browser wait --load networkidle
# agent-browser snapshot -i
#
# # Fill credentials (update refs to match your form)
# agent-browser fill @e1 "$APP_USERNAME"
# agent-browser fill @e2 "$APP_PASSWORD"
# agent-browser click @e3
# agent-browser wait --load networkidle
#
# # Verify login succeeded
# FINAL_URL=$(agent-browser get url)
# if [[ "$FINAL_URL" == *"login"* ]] || [[ "$FINAL_URL" == *"signin"* ]]; then
# echo "Login failed - still on login page"
# agent-browser screenshot /tmp/login-failed.png
# agent-browser close
# exit 1
# fi
#
# # Save state for future runs
# echo "Saving state to $STATE_FILE"
# agent-browser state save "$STATE_FILE"
# echo "Login successful"
# agent-browser snapshot -i
#!/bin/bash
# Template: Content Capture Workflow
# Purpose: Extract content from web pages (text, screenshots, PDF)
# Usage: ./capture-workflow.sh <url> [output-dir]
#
# Outputs:
# - page-full.png: Full page screenshot
# - page-structure.txt: Page element structure with refs
# - page-text.txt: All text content
# - page.pdf: PDF version
#
# Optional: Load auth state for protected pages
set -euo pipefail
TARGET_URL="${1:?Usage: $0 <url> [output-dir]}"
OUTPUT_DIR="${2:-.}"
echo "Capturing: $TARGET_URL"
mkdir -p "$OUTPUT_DIR"
# Optional: Load authentication state
# if [[ -f "./auth-state.json" ]]; then
# echo "Loading authentication state..."
# agent-browser state load "./auth-state.json"
# fi
# Navigate to target
agent-browser open "$TARGET_URL"
agent-browser wait --load networkidle
# Get metadata
TITLE=$(agent-browser get title)
URL=$(agent-browser get url)
echo "Title: $TITLE"
echo "URL: $URL"
# Capture full page screenshot
agent-browser screenshot --full "$OUTPUT_DIR/page-full.png"
echo "Saved: $OUTPUT_DIR/page-full.png"
# Get page structure with refs
agent-browser snapshot -i > "$OUTPUT_DIR/page-structure.txt"
echo "Saved: $OUTPUT_DIR/page-structure.txt"
# Extract all text content
agent-browser get text body > "$OUTPUT_DIR/page-text.txt"
echo "Saved: $OUTPUT_DIR/page-text.txt"
# Save as PDF
agent-browser pdf "$OUTPUT_DIR/page.pdf"
echo "Saved: $OUTPUT_DIR/page.pdf"
# Optional: Extract specific elements using refs from structure
# agent-browser get text @e5 > "$OUTPUT_DIR/main-content.txt"
# Optional: Handle infinite scroll pages
# for i in {1..5}; do
# agent-browser scroll down 1000
# agent-browser wait 1000
# done
# agent-browser screenshot --full "$OUTPUT_DIR/page-scrolled.png"
# Cleanup
agent-browser close
echo ""
echo "Capture complete:"
ls -la "$OUTPUT_DIR"
#!/bin/bash
# Template: Form Automation Workflow
# Purpose: Fill and submit web forms with validation
# Usage: ./form-automation.sh <form-url>
#
# This template demonstrates the snapshot-interact-verify pattern:
# 1. Navigate to form
# 2. Snapshot to get element refs
# 3. Fill fields using refs
# 4. Submit and verify result
#
# Customize: Update the refs (@e1, @e2, etc.) based on your form's snapshot output
set -euo pipefail
FORM_URL="${1:?Usage: $0 <form-url>}"
echo "Form automation: $FORM_URL"
# Step 1: Navigate to form
agent-browser open "$FORM_URL"
agent-browser wait --load networkidle
# Step 2: Snapshot to discover form elements
echo ""
echo "Form structure:"
agent-browser snapshot -i
# Step 3: Fill form fields (customize these refs based on snapshot output)
#
# Common field types:
# agent-browser fill @e1 "John Doe" # Text input
# agent-browser fill @e2 "user@example.com" # Email input
# agent-browser fill @e3 "SecureP@ss123" # Password input
# agent-browser select @e4 "Option Value" # Dropdown
# agent-browser check @e5 # Checkbox
# agent-browser click @e6 # Radio button
# agent-browser fill @e7 "Multi-line text" # Textarea
# agent-browser upload @e8 /path/to/file.pdf # File upload
#
# Uncomment and modify:
# agent-browser fill @e1 "Test User"
# agent-browser fill @e2 "test@example.com"
# agent-browser click @e3 # Submit button
# Step 4: Wait for submission
# agent-browser wait --load networkidle
# agent-browser wait --url "**/success" # Or wait for redirect
# Step 5: Verify result
echo ""
echo "Result:"
agent-browser get url
agent-browser snapshot -i
# Optional: Capture evidence
agent-browser screenshot /tmp/form-result.png
echo "Screenshot saved: /tmp/form-result.png"
# Cleanup
agent-browser close
echo "Done"
agent-browser 安装与配置手册
浏览器自动化 CLI 工具,专为 AI Agent 设计。支持 macOS、Windows、Linux。
目录
---
系统要求
| 项目 | 要求 |
|---|---|
| Node.js | 16+(推荐 18+) |
| 操作系统 | macOS 10.15+、Windows 10/11、Ubuntu 20.04+ / Debian 11+ |
| 架构 | x64 或 ARM64 |
| 磁盘空间 | 约 500MB(含 Chromium) |
---
安装
macOS
方式一:npm 全局安装(推荐)
npm install -g agent-browser
agent-browser install # 下载 Chromium方式二:Homebrew
brew install agent-browser
agent-browser install方式三:npx 免安装试用
npx agent-browser install # 首次下载 Chromium
npx agent-browser open https://example.comnpx 方式每次经过 Node.js 中转,速度比全局安装慢。日常使用建议全局安装。
Node.js 未安装?
推荐使用 nvm 管理:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.zshrc
nvm install --lts---
Windows
方式一:npm 全局安装(推荐)
打开 PowerShell(建议以管理员身份运行):
npm install -g agent-browser
agent-browser install # 下载 Chromium方式二:npx 免安装试用
npx agent-browser install
npx agent-browser open https://example.comNode.js 未安装?
从官网下载安装包:https://nodejs.org/
安装完成后重新打开终端,验证:
node --version
npm --versionPowerShell 执行策略
如果遇到"禁止运行脚本"错误:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser---
Linux
方式一:npm 全局安装
npm install -g agent-browser
agent-browser install --with-deps # 下载 Chromium 并安装系统依赖方式二:手动安装系统依赖
npm install -g agent-browser
agent-browser install
npx playwright install-deps chromium # 安装系统库Node.js 未安装?
# Ubuntu / Debian
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
# 或使用 nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts---
验证安装
所有平台通用:
agent-browser --version快速功能测试:
agent-browser open https://example.com
agent-browser snapshot -i
agent-browser screenshot /tmp/test.png # Windows: $env:TEMP\test.png
agent-browser close---
平台差异速查
| 操作 | macOS / Linux | Windows (PowerShell) |
|---|---|---|
| 用户主目录 | ~ 或 $HOME | $env:USERPROFILE 或 $HOME |
| 临时目录 | /tmp | $env:TEMP |
| 桌面路径 | ~/Desktop | $env:USERPROFILE\Desktop |
| 路径分隔符 | / | \ 或 /(均可) |
| Shell 配置文件 | ~/.zshrc 或 ~/.bashrc | PowerShell Profile |
| 环境变量(临时) | export KEY=value | $env:KEY = "value" |
| 环境变量(永久) | 写入 shell 配置文件 | [Environment]::SetEnvironmentVariable("KEY","value","User") |
| 查找命令路径 | which agent-browser | Get-Command agent-browser |
| 进程查看 | `ps aux \ | grep chromium` |
---
基本使用
每个浏览器自动化任务遵循这个流程:
打开网页 → 快照获取元素引用 → 交互操作 → 重新快照 → 关闭# 1. 打开网页
agent-browser open https://example.com
# 2. 获取可交互元素(返回 @e1, @e2 等引用)
agent-browser snapshot -i
# 3. 用引用操作元素
agent-browser click @e1
agent-browser fill @e2 "hello"
# 4. 页面变化后必须重新快照
agent-browser snapshot -i
# 5. 完成后关闭
agent-browser close重要:引用(@e1,@e2)在页面变化后失效,必须重新snapshot -i获取新引用。
---
命令速查表
导航
| 命令 | 说明 |
|---|---|
agent-browser open <url> | 打开网页 |
agent-browser back | 后退 |
agent-browser forward | 前进 |
agent-browser reload | 刷新 |
agent-browser close | 关闭浏览器 |
快照与交互
| 命令 | 说明 |
|---|---|
agent-browser snapshot -i | 获取可交互元素及引用 |
agent-browser click @e1 | 点击 |
agent-browser fill @e2 "text" | 清空并输入 |
agent-browser type @e2 "text" | 追加输入(不清空) |
agent-browser select @e1 "value" | 下拉选择 |
agent-browser check @e1 | 勾选复选框 |
agent-browser press Enter | 按键 |
agent-browser scroll down 500 | 滚动 |
agent-browser hover @e1 | 悬停 |
获取信息
| 命令 | 说明 |
|---|---|
agent-browser get text @e1 | 获取元素文本 |
agent-browser get url | 获取当前 URL |
agent-browser get title | 获取页面标题 |
agent-browser get value @e1 | 获取输入框值 |
等待
| 命令 | 说明 |
|---|---|
agent-browser wait @e1 | 等待元素出现 |
agent-browser wait --load networkidle | 等待网络空闲 |
agent-browser wait --url "**/page" | 等待 URL 匹配 |
agent-browser wait --text "成功" | 等待文本出现 |
agent-browser wait 3000 | 等待 3 秒 |
截图与导出
| 命令 | 说明 |
|---|---|
agent-browser screenshot file.png | 截图 |
agent-browser screenshot --full file.png | 全页截图 |
agent-browser pdf file.pdf | 导出 PDF |
语义定位器(无需引用)
agent-browser find text "登录" click
agent-browser find label "邮箱" fill "user@test.com"
agent-browser find role button click --name "提交"
agent-browser find placeholder "搜索" type "关键词"---
常用场景
表单填写
agent-browser open https://example.com/form
agent-browser snapshot -i
# 输出: @e1 [input "姓名"], @e2 [input "邮箱"], @e3 [button "提交"]
agent-browser fill @e1 "张三"
agent-browser fill @e2 "zhangsan@example.com"
agent-browser click @e3
agent-browser wait --load networkidle
agent-browser snapshot -i # 查看结果
agent-browser close登录并保存会话
agent-browser open https://app.example.com/login
agent-browser snapshot -i
agent-browser fill @e1 "$USERNAME" # macOS/Linux
agent-browser fill @e2 "$PASSWORD"
# Windows PowerShell: agent-browser fill @e1 $env:USERNAME
agent-browser click @e3
agent-browser wait --url "**/dashboard"
agent-browser state save auth-state.json
agent-browser close
# 下次直接加载
agent-browser state load auth-state.json
agent-browser open https://app.example.com/dashboard数据提取
agent-browser open https://example.com/products
agent-browser snapshot -i
agent-browser get text @e5 # 单个元素
agent-browser get text body > content.txt # 整页文本
agent-browser get text @e1 --json # JSON 格式
agent-browser closeJavaScript 执行
# 简单表达式
agent-browser eval 'document.title'
# 复杂脚本用 heredoc(macOS/Linux)
agent-browser eval --stdin <<'EOF'
Array.from(document.querySelectorAll('a'))
.map(a => ({ text: a.textContent, href: a.href }))
EOF
# Windows PowerShell 用管道
'document.querySelectorAll("img").length' | agent-browser eval --stdin---
会话与状态管理
命名会话(隔离 cookies 和存储)
agent-browser --session site1 open https://site-a.com
agent-browser --session site2 open https://site-b.com
agent-browser session list
agent-browser --session site1 close自动持久化会话
agent-browser --session-name myapp open https://app.example.com
# ... 登录操作 ...
agent-browser close # 状态自动保存到 ~/.agent-browser/sessions/
# 下次自动恢复
agent-browser --session-name myapp open https://app.example.com手动保存/加载状态
agent-browser state save auth.json
agent-browser state load auth.json
agent-browser state list
agent-browser state clear myapp
agent-browser state clean --older-than 7 # 清理 7 天前的状态加密会话
# macOS/Linux
export AGENT_BROWSER_ENCRYPTION_KEY=$(openssl rand -hex 32)
# Windows PowerShell
$env:AGENT_BROWSER_ENCRYPTION_KEY = -join ((1..32) | ForEach-Object { '{0:x2}' -f (Get-Random -Max 256) })
agent-browser --session-name secure open https://app.example.com---
高级功能
可视化调试
agent-browser --headed open https://example.com # 显示浏览器窗口
agent-browser highlight @e1 # 高亮元素视频录制
agent-browser record start demo.webm
# ... 执行操作 ...
agent-browser record stop设备模拟
agent-browser set viewport 1920 1080
agent-browser set device "iPhone 14"
agent-browser set media dark # 深色模式网络控制
agent-browser --proxy http://proxy:8080 open https://example.com
agent-browser network route "*/ads/*" --abort # 拦截广告
agent-browser network route "*/api/data" --body '{"mock": true}' # 模拟响应iOS Simulator(仅 macOS)
需要 Xcode 和 Appium:
npm install -g appium && appium driver install xcuitest
agent-browser device list
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 swipe up
agent-browser -p ios screenshot mobile.png
agent-browser -p ios close连接已有 Chrome
agent-browser --auto-connect open https://example.com
# 或指定 CDP 端口
agent-browser --cdp 9222 snapshot打开本地文件
agent-browser --allow-file-access open file:///path/to/document.pdf
agent-browser --allow-file-access open file:///path/to/page.html---
在 CountBot 中使用
前置条件
确保 agent-browser 已全局安装并可用:
agent-browser --version工作方式
CountBot 的 AI Agent 通过 Shell 工具直接调用 agent-browser 命令。skills/agent-browser/SKILL.md 已配置:
allowed-tools: Bash(npx agent-browser:*), Bash(agent-browser:*)对话示例
用户: 帮我打开 example.com 并截图
AI: [调用 agent-browser open / screenshot / close]
截图已保存。
用户: 帮我登录 GitHub,查看我的 star 列表
AI: [调用一系列 agent-browser 命令完成登录和数据提取]集成测试
python3 tests/test_agent_browser_integration.py---
平台特定注意事项
macOS
| 问题 | 解决方案 |
|---|---|
| Gatekeeper 阻止 Chromium | xattr -d com.apple.quarantine ~/.cache/ms-playwright/chromium-*/chrome-mac/Chromium.app |
| 需要屏幕录制权限 | 系统设置 → 隐私与安全性 → 屏幕录制 → 添加终端 |
| Apple Silicon 架构确认 | file $(which agent-browser) 应显示 arm64 |
| 使用 nvm 时路径问题 | 确保 nvm use <版本> 后再安装 |
| 公司网络代理 | agent-browser --proxy http://proxy:8080 open <url> |
zsh 快捷别名(可选):
# 添加到 ~/.zshrc
alias ab='agent-browser'
alias abo='agent-browser open'
alias abs='agent-browser snapshot -i'
alias abc='agent-browser close'Windows
| 问题 | 解决方案 |
|---|---|
| 执行策略限制 | Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser |
| 防火墙弹窗 | 点击"允许访问",或添加规则:New-NetFirewallRule -DisplayName "agent-browser" -Direction Inbound -Program "$env:APPDATA\npm\node_modules\agent-browser\node_modules\playwright\*" -Action Allow |
| 杀毒软件误报 | 将 %LOCALAPPDATA%\ms-playwright 和 %APPDATA%\npm\node_modules\agent-browser 加入白名单 |
| 长路径限制 | 注册表:HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem 设置 LongPathsEnabled = 1 |
| 中文路径编码 | PowerShell 中执行 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 |
性能优化(可选):
# 排除 Chromium 目录的实时扫描
Add-MpPreference -ExclusionPath "$env:LOCALAPPDATA\ms-playwright"Linux
| 问题 | 解决方案 |
|---|---|
| 缺少系统依赖 | agent-browser install --with-deps 或 npx playwright install-deps chromium |
| 无头服务器无显示 | agent-browser 默认 headless 模式,无需 X11 |
| Docker 中运行 | 需要 --no-sandbox 或以非 root 用户运行 |
---
故障排查
通用问题
| 症状 | 原因 | 解决 |
|---|---|---|
Daemon not found | npm 包未正确安装 | npm install -g agent-browser |
| 页面加载超时 | 网络慢或页面复杂 | 在 open 后加 wait --load networkidle |
引用 @e1 无效 | 页面变化后引用失效 | 重新执行 snapshot -i |
command not found | 未安装或不在 PATH 中 | 检查 which agent-browser(macOS/Linux)或 Get-Command agent-browser(Windows) |
| Chromium 启动失败 | 浏览器未下载 | agent-browser install |
查看详细日志
# macOS / Linux
DEBUG=pw:* agent-browser open https://example.com
# Windows PowerShell
$env:DEBUG = "pw:*"
agent-browser open https://example.com重新安装
npm uninstall -g agent-browser
npm install -g agent-browser
agent-browser install验证脚本
Bash(macOS / Linux)
#!/bin/bash
set -e
echo "检查安装..."
agent-browser --version || { echo "❌ 未安装"; exit 1; }
echo "打开网页..."
agent-browser open https://example.com
echo "快照..."
agent-browser snapshot -i
echo "关闭..."
agent-browser close
echo "✅ 所有测试通过"PowerShell(Windows)
Write-Host "检查安装..."
agent-browser --version
if ($LASTEXITCODE -ne 0) { Write-Host "❌ 未安装"; exit 1 }
Write-Host "打开网页..."
agent-browser open https://example.com
Write-Host "快照..."
agent-browser snapshot -i
Write-Host "关闭..."
agent-browser close
Write-Host "✅ 所有测试通过"Python(跨平台)
python3 tests/test_agent_browser_integration.py---
卸载
macOS / Linux
npm uninstall -g agent-browser
rm -rf ~/.cache/ms-playwright
rm -rf ~/.agent-browser
# Homebrew 安装的
brew uninstall agent-browserWindows
npm uninstall -g agent-browser
Remove-Item -Recurse -Force "$env:LOCALAPPDATA\ms-playwright"
Remove-Item -Recurse -Force "$env:USERPROFILE\.agent-browser"---
环境变量(可选)
| 变量 | 说明 | 示例 |
|---|---|---|
AGENT_BROWSER_SESSION | 默认会话名 | mysession |
AGENT_BROWSER_EXECUTABLE_PATH | 自定义浏览器路径 | /usr/bin/google-chrome |
AGENT_BROWSER_EXTENSIONS | 加载浏览器扩展 | /path/ext1,/path/ext2 |
AGENT_BROWSER_HOME | 自定义安装位置 | 通常不需要设置 |
AGENT_BROWSER_ENCRYPTION_KEY | 会话加密密钥 | 64 位十六进制字符串 |
---
参考资料
| 资源 | 链接 |
|---|---|
| 官方网站 | https://agent-browser.dev/ |
| GitHub 仓库 | https://github.com/vercel-labs/agent-browser |
| npm 包 | https://www.npmjs.com/package/agent-browser |
| 完整命令参考 | references/commands.md |
| 会话管理 | references/session-management.md |
| 认证处理 | references/authentication.md |
| 引用生命周期 | references/snapshot-refs.md |
| 视频录制 | references/video-recording.md |
| 代理配置 | references/proxy-support.md |
| AI 技能定义 | SKILL.md |
| 模板脚本 | templates/ |