Browser Automation with agent-browser
Overview
Automate browser interactions using the agent-browser CLI tool. Every automation follows a consistent navigate → snapshot → interact → re-snapshot pattern. The browser persists between commands via a background daemon, making command chaining efficient.
Setup — run this FIRST (self-installing)
The agent-browser CLI is not preinstalled. This skill ships bootstrap.sh,
which is idempotent — run it at the start of every task; it no-ops if already set up:
# install the CLI + ensure Chromium, and export PATH/PNPM_HOME/PLAYWRIGHT_BROWSERS_PATH
source "$(dirname "$0")/bootstrap.sh" 2>/dev/null || source ./bootstrap.sh
# (the skill dir is printed by `agent-browser skills path` once installed)
If you only need the binary on PATH (no env persistence), run it as a subprocess:
bash <skill-dir>/bootstrap.sh.
What bootstrap handles (and why — validated 2026-06-10)
- Install with
pnpm, notnpm. The bundled npm (Pythonnodejs_wheel) is broken here.pnpm add -g agent-browser(public npm registry) works. pnpm prints "Ignored build scripts" — harmless. The bin lands in$PNPM_HOME(default~/.local/share/pnpm), which bootstrap adds toPATH. - Playwright Chromium is usually already cached at
~/.cache/ms-playwright(PLAYWRIGHT_BROWSERS_PATH). Bootstrap only downloads it if absent.
Headless and managed environments
- Headless is the default — do not pass
--headedon a server with no display. - Container sandboxing is policy-owned. Keep Chromium's sandbox enabled unless the AgentConfig browser profile explicitly selects an already-isolated container runtime that requires a different setting. Do not embed runtime flags in the skill.
- Private endpoints are reference-driven. Resolve the target URL and TLS profile from AgentConfig. Install the referenced complete CA bundle and keep HTTPS upgrade, certificate, and hostname verification enabled. A plain-HTTP development endpoint requires an explicit transport policy and must not become a stored default.
- Connection flags are profile-owned. When a managed CDP browser is required, use the browser connection reference supplied by the runtime. Do not retain its address, user-data path, or launch command.
Daemon hygiene
- Reset the persistent browser with
agent-browser close --all. - Do NOT
pkill -f agent-browser— the pattern matches your own shell/command line and kills the session (seen as exit 144). Kill a pre-launched Chromium by its unique--user-data-dirpath instead (e.g.pkill -f /tmp/agent-browser-prof). - A stale daemon is a common cause of
Multiple targets are not supported in headless mode;close --all(and, if needed, killing the CDP Chromium) clears it.
Core Workflow
Every browser automation task follows this pattern:
- Navigate:
agent-browser open <url> - Snapshot:
agent-browser snapshot -i(get interactive element refs like@e1,@e2) - Interact: Use refs to click, fill, select
- 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 result
Command Reference
# 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)
agent-browser snapshot -s "#selector" # Scope to CSS selector
# Interaction (use @refs from snapshot)
agent-browser click @e1 # Click element
agent-browser click @e1 --new-tab # Click and open in new tab
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 screenshot --annotate # Annotated screenshot with numbered labels
agent-browser pdf output.pdf # Save as PDF
# Diff (compare page states)
agent-browser diff snapshot # Compare current vs last snapshot
agent-browser diff snapshot --baseline before.txt # Compare current vs saved file
agent-browser diff screenshot --baseline before.png # Visual pixel diff
agent-browser diff url <url1> <url2> # Compare two pages
Common 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 networkidle
Authentication 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/dashboard
Session 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
Data 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 --json
Parallel 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 list
Command Chaining
Commands can be chained with && in a single shell invocation. The browser persists between commands, so chaining is safe and efficient.
# Chain open + wait + snapshot in one call
agent-browser open https://example.com && agent-browser wait --load networkidle && agent-browser snapshot -i
# Navigate and capture
agent-browser open https://example.com && agent-browser wait --load networkidle && agent-browser screenshot page.png
When to chain: Use && when intermediate output isn't needed (e.g., open + wait + screenshot). Run commands separately when parsing output first (e.g., snapshot to discover refs, then interact using those refs).
Ref Lifecycle
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 refs
Annotated Screenshots (Vision Mode)
Use --annotate to take a screenshot with numbered labels overlaid on interactive elements. Each label [N] maps to ref @eN. This also caches refs, so interactions can begin immediately without a separate snapshot.
agent-browser screenshot --annotate
# Output includes the image path and a legend:
# [1] @e1 button "Submit"
# [2] @e2 link "Home"
# [3] @e3 textbox "Email"
agent-browser click @e2 # Click using ref from annotated screenshot
Use annotated screenshots when:
- The page has unlabeled icon buttons or visual-only elements
- Canvas or chart elements are present (invisible to text snapshots)
- Spatial reasoning about element positions is needed
Semantic 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" click
JavaScript Evaluation
Use eval to run JavaScript in the browser context. Use --stdin or -b for complex expressions to avoid shell quoting issues.
# Simple expressions
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
# Base64 encoding (avoids all shell escaping issues)
agent-browser eval -b "$(echo -n 'Array.from(document.querySelectorAll("a")).map(a => a.href)' | base64)"
Timeouts and Slow Pages
# 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"
# Wait for a URL pattern (useful after redirects)
agent-browser wait --url "**/dashboard"
# Wait for a JavaScript condition
agent-browser wait --fn "document.readyState === 'complete'"
# Fixed duration as a last resort
agent-browser wait 5000
Diffing (Verifying Changes)
Use diff snapshot after an action to verify it had the intended effect:
# Typical workflow: snapshot → action → diff
agent-browser snapshot -i # Take baseline snapshot
agent-browser click @e2 # Perform action
agent-browser diff snapshot # See what changed
# Visual regression testing
agent-browser screenshot baseline.png
# ... changes made ...
agent-browser diff screenshot --baseline baseline.png
# Compare staging vs production
agent-browser diff url https://staging.example.com https://prod.example.com --screenshot
Best Practices
- Always re-snapshot after navigation — refs become invalid when the page changes
- Use
wait --load networkidleafter opening slow or SPA-based pages - Use named sessions when running concurrent agents to avoid conflicts
- Always close sessions when done to avoid leaked browser processes
- Prefer
--stdinfor complex JavaScript to avoid shell escaping issues