browser-agent — drives a real Chrome window from the terminal and answers in JSON, not screenshots. # browser-agent **A Claude Code skill that drives a real Chrome window from the terminal —
and answers in JSON, not screenshots.** [![license](https://img.shields.io/badge/license-MIT-3fb950.svg)](LICENSE) [![platform](https://img.shields.io/badge/platform-Windows-0078D6.svg)](#portability) [![python](https://img.shields.io/badge/python-3.9%2B-3776AB.svg)](#requirements) [![claude code](https://img.shields.io/badge/Claude%20Code-skill-D97757.svg)](https://claude.com/claude-code) [Install](#install) · [How it works](#the-loop) · [Commands](#commands) · [Architecture](skills/browser-agent/reference/ARCHITECTURE.md) · **English** / [Türkçe](README.tr.md)
A terminal session: open a page, snapshot it as refs, fill two fields, diff the snapshot — the submit button loses its disabled state, for about 40 tokens.

## The problem A coding agent that needs to check a web page reaches for a screenshot. A full-page screenshot costs **1,500–4,800 tokens every single time it looks** — and it comes back as pixels, so the agent then has to guess coordinates to click anything. This skill's default is a text snapshot of the page's interactive elements: **150–500 tokens**, with stable handles you can click and fill by name. Screenshots stay available for when you genuinely need eyes on something — scoped to a single element where possible.
Token cost comparison: full page raw ~4800, full page budgeted ~1500, viewport ~700, snap ~300, single element ~21.
Built to be handed to a **cheap, fast subagent**: it runs the browser loop and reports back a few lines, so your main context never fills up with page dumps. --- ## Requirements - **Windows** 10/11 — the only platform this has been tested on, see [Portability](#portability) - **Python** 3.9+ on `PATH` - **Google Chrome** or Microsoft Edge ## Install ```powershell git clone https://github.com/azderus/browser-agent.git cd browser-agent .\install.ps1 ``` `install.ps1` copies `skills/browser-agent` into `%USERPROFILE%\.claude\skills\`, creates a shared virtualenv at `%LOCALAPPDATA%\browser-agent\venv`, and runs a health check. Nothing is installed into your project.
Other ways to install
Per-project instead of globally: ```powershell .\install.ps1 -Scope project # -> .\.claude\skills\browser-agent .\install.ps1 -Dest C:\somewhere # -> explicit path ``` Or just copy the `skills/browser-agent` folder anywhere and run its `setup.ps1`. It has no dependency on its own location — the launcher resolves paths relative to itself. `playwright install` is deliberately **not** run. We attach to the Chrome already on your machine over CDP, so there is no reason to download a second Chromium (~150MB).
Verify: ```powershell %USERPROFILE%\.claude\skills\browser-agent\ba.cmd doctor ``` ```json {"checks":{"version":"1.1.0","python":"3.14.3","chrome":"C:\\...\\chrome.exe", "playwright":"kurulu","pillow":"12.3.0","cdpPort":9222,"cdpAlive":false, "profile":"C:\\...\\browser-agent\\profiles\\9222","dialogPolicy":"auto"},"ok":true} ``` Then just ask Claude Code to look at a page — the skill loads on its own. Or use it straight from a shell; it is a normal CLI. --- ## The loop ``` open → navigate (RESETS all refs — snap immediately after) snap → list interactive elements as refs (e1, e2, …) click e3 | fill e1 "…" | select e2 "value" | press Enter snap --diff → after an action, see ONLY what changed logs → console + network errors ``` Every command prints **one line of JSON**. Failures are JSON too — raw tracebacks never reach stdout: ```json {"ok":false,"error":"e7 sayfada yok","code":"stale_ref","retryable":true, "hint":"once snap calistir","next":"snap"} ``` `error` and `hint` are Turkish text for a human to read. `code` is a fixed ASCII identifier, and it is the field to branch on — never the message text. `retryable` says whether repeating the command can help, and `next` names the command that usually fixes it. Invalid commands and arguments return this shape too (`code: "bad_usage"`), on stdout, with exit code 2. ### Refs, not coordinates `snap` writes a `data-ba-ref` attribute onto each visible interactive element and returns a short list. Later commands address `e1`, `e4`, and so on. Because the attributes live in the DOM, a re-render wipes them — **and that is deliberate**. The next command fails with *"run snap first"* instead of silently clicking whatever moved into those coordinates. Ref numbers are reassigned on each snap, so always act on the most recent one. Each snap also stamps the page with an id, and a ref command checks it before acting: if the page has been re-rendered since, you get `stale_snapshot` rather than a click on whatever element happens to hold that number now. In `--diff` output, element identity is `role + name + state` rather than the ref number, which is why enabling a button shows up as a remove/add pair. ### Canvas and images Non-text visuals (`canvas`, `svg`, `img`, `video`) are listed separately under `media` with their own refs, plus a hint saying: this is pixels, not text — screenshot this one element. Reading a verification code off a canvas costs **~30 tokens** that way, versus ~1,500 for a full-page capture. --- ## Commands ``` start | stop | state | doctor open [--settle ms] snap [--filter ] [--diff] [--max N] [--all] click [--dblclick] [--force] fill [--text-file F] [--enter] [--append] select press [--ref R] hover scroll [amount] [--ref R] back | forward | reload wait [--text T] [--ref R] [--ms N] text [--ref R] [--selector CSS] [--chars N] logs [--all] [--limit N] [--keep] shot [--ref R] [--selector CSS] [--full] [--width PX] [--tokens N] [--jpeg] js tab [index] ``` Global flags — `--port N` (default 9222), `--timeout MS`, `--headless` — work **before or after** the subcommand: `shot --ref e3 --timeout 20000` is valid. 📖 **[Full command reference →](skills/browser-agent/reference/COMMANDS.md)**   🏗 **[Design notes →](skills/browser-agent/reference/ARCHITECTURE.md)** --- ## Why not just install Playwright and let the agent write scripts? You can, and for a one-off it is fine. This exists because the repeated case has sharp edges that a generated script hits every single time: | Trap | What happens without handling | |---|---| | Chrome 136+ refuses `--remote-debugging-port` on the default profile | The debug port silently never opens | | Each CLI command is a separate process | The browser cannot be held in memory between commands | | Playwright listeners die with the connection | Console errors from earlier commands are lost | | Chrome stops compositing when its window is minimized | `captureScreenshot` hangs until timeout, while `js` keeps working | | `alert`/`confirm` block the page | Every subsequent command times out | | PowerShell prepends a UTF-8 BOM when piping to a native exe | An invisible character lands in the form field | All of it is handled once here, instead of being rediscovered per script. The dedicated `--user-data-dir` is also the safe choice: the agent gets an isolated profile, not your logged-in accounts. --- ## Where things are written ``` %LOCALAPPDATA%\browser-agent\ profiles\\ dedicated Chrome profile, one per port (persistent logins) sessions\\ state.json current tab, log buffer runs\ screenshots and text dumps (last 40, 200 MB cap) ``` If an old `profile\` folder from a pre-1.1 install is still there, port 9222 keeps using it, so logins you set up by hand are not lost. Every other port uses the new layout. Nothing lands in your project directory. If you drop the skill folder somewhere under a web root, the bundled `.htaccess` denies Apache access to it. Run several projects side by side with `--port`: each port gets its own state directory, its own output directory **and its own profile**, so the sessions are genuinely independent — separate cookies, separate logins, separate history. Before 1.1 all ports shared a single profile, which meant a second port could not start at all: Chrome refuses to launch a second instance against the same `--user-data-dir`. --- ## Safety and privacy Plain facts, worth knowing before you point this at a real site: - **The profile persists on disk.** The tool drives a dedicated Chrome profile at `%LOCALAPPDATA%\browser-agent\profiles\`, and that profile keeps cookies and logged-in sessions. If you log into a site through it, that session stays there until you delete the folder — and any later command on the same port is logged in as you. - **Run artifacts contain page content.** The files under `sessions\\runs\` are page text and screenshots, so they hold whatever was on screen when they were taken. - **`js` executes arbitrary code in the page**, with that page's cookies and origin. The profile is separate from your everyday Chrome profile, so the agent does not see the accounts you are logged into there. --- ## Example [`examples/wizard.html`](examples/wizard.html) is a deliberately hostile three-step registration form, written to break the tool. It found six real defects.
What it throws at an agent
| Obstacle | What it tests | |---|---| | Submit disabled until email has `@` and password ≥ 8 chars | Reading `disabled` instead of clicking hopefully | | Step 1 → 2 goes through a 1.4s spinner | `wait`, rather than acting on a stale snapshot | | A field that only appears for one dropdown value | Re-snapping after a conditional render | | Hidden decoy button | Not clicking invisible elements | | Full `innerHTML` replacement between steps | Refs dying, and failing loudly instead of misclicking | | `confirm()` on submit | Dialog handling | | **A 4-digit code drawn only onto a ``** | The one that matters — text extraction cannot reach it | The success screen shows a reference number derived from the canvas code (`'RX-' + (code * 7).toString(36)`). Since the code is random per load and lives only in closure scope — not on `window`, not in the DOM — an agent that reports a valid reference number **provably read the canvas** rather than guessing.
--- ## A note on language Documentation is available in [English](README.md) and [Türkçe](README.tr.md). The tool's own JSON messages and source comments are **Turkish** — it was written that way, and translating the strings would mean re-testing every error path. Nothing depends on reading them: every failure also carries a stable ASCII `code`, and `SKILL.md` — which the agent reads — documents the full list and tells it to branch on `code`, never on the message text. A few messages you will see: | Message | Meaning | |---|---| | `tarayici calismiyor` | browser is not running | | `once snap calistir` | run `snap` first (refs are stale) | | `e3 sayfada yok` | ref `e3` is no longer in the page | | `zaman asimi` | timeout | | `kapatildi` | closed | PRs translating them are welcome. ## Portability Windows only, and honestly so. The platform-specific parts are small and isolated: the Chrome path list in `find_chrome()`, the data directory constant `ROOT`, the `taskkill` fallback in `cmd_stop()`, and the `ba.cmd` / `setup.ps1` launchers. A macOS/Linux port is mostly a matter of extending those four and adding a `setup.sh`. PRs welcome — but please only claim a platform you have actually run it on. ## Caveats - ~1.5–2s of fixed overhead per command (Python + Playwright driver startup) - `js` runs arbitrary code in the page. This is a local development tool. - The state file is not locked; do not run two commands against the same port at once - Arguments containing `&`, `<`, `>` can be mangled by `cmd` — use `--text-file` or pipe via stdin (`-`) ## Changes in 1.1 Two of these change existing behaviour. Read them first. **Breaking** - **`confirm()` and `prompt()` are now dismissed, not accepted.** Previously every dialog was accepted unconditionally, so clicking a button could silently confirm "Are you sure you want to delete everything?". Under the new `--dialog auto` default, `alert` and `beforeunload` are still accepted — one has no other answer, and refusing the other locks navigation — while `confirm` and `prompt` are dismissed. Pass `--dialog accept` to get the old behaviour, which is what a flow that has to get past a confirmation now needs, or `--dialog dismiss` to refuse all four. The `dialogs` array in the output says which way each one went. - **`stop` will not close a browser it did not start.** `start` and `open` now record the PID, the WebSocket debugger URL and the profile path; `stop` compares them against the live browser on the port and returns `code: "port_not_ours"` on a mismatch instead of killing someone else's Chrome. `stop --force` skips the check. **Fixes** - `--port` now gives genuinely independent sessions, one profile per port. A second port used to fail to start at all. - `select` matches on the option's `value` first, then its label. `select e3 tr` against `` used to fail. The output reports which one matched; no match returns `no_match` plus the available options. - A ref command detects a stale snapshot: if the page was re-rendered after the last `snap`, you get `stale_snapshot` even when the old ref number happens to match a different element. - Navigation (`open`, `back`, `forward`, `reload`, `tab`) now clears the snapshot baseline completely. It used to keep the element signatures, so a `--diff` across two different pages compared them against each other. - `--diff` counts repeated elements individually — ten identical "Delete" buttons no longer collapse into one — reports `unchanged` correctly, and flags `baselinePartial` when the baseline came from a `--filter` or `--max` snap. - Invalid commands and arguments return JSON on stdout instead of an argparse usage message on stderr. - Every error carries a machine-readable `code` field. Agent recovery logic should branch on that, not on the Turkish message text. ## License MIT © [azderus](https://github.com/azderus) — see [LICENSE](LICENSE).