# llm-browser-agent **Self-healing browser automation. No more `page.click("#btn-x92-final-v2")` breaking every time a site ships a redesign.** [![Tests](https://github.com/justifysiddh/llm-browser-agent/actions/workflows/tests.yml/badge.svg)](https://github.com/justifysiddh/llm-browser-agent/actions) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE) [![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/) ```python agent.do("click the 'Add to cart' button") agent.do("type my email into the newsletter signup field") agent.do("select 'Large' from the size dropdown") ``` That's it. No selectors. No XPath. No 3am Slack alert because a `data-testid` got renamed. --- ## The problem Traditional browser automation is brittle by design: ```python # This breaks the moment the site's build pipeline changes a class name page.click("div.MuiButton-root.css-1x92jd3 > span:nth-child(2)") ``` Every CI pipeline running Selenium/Playwright tests has felt this pain. Site redesigns, A/B tests, and framework migrations all silently break selectors — and nobody notices until a test suite goes red for the wrong reason. ## How it works 1. **Compress** — the live DOM (often 500KB+) is walked and compressed into a compact, token-efficient JSON tree: tag, role, visible text, accessible name, and stable attributes only. Layout noise is stripped. 2. **Resolve** — the compressed tree + your natural-language instruction go to an LLM, which returns a structured `{ref, action, value, confidence}` — never free text, so execution stays deterministic. 3. **Execute with fallback** — the target element is resolved through a priority chain: `data-testid` → `id` → `role+name` → `text` → `bounding-box click`. If a strategy fails (element gone, renamed, restructured), it falls back automatically — this is the "self-healing" part. 4. **Retry with context** — if execution still fails, the DOM is re-snapshotted and the LLM gets a second attempt with the failure reason attached. ``` Natural language instruction │ ▼ ┌─────────────────┐ ┌──────────────────┐ ┌───────────────────┐ │ DOMCompressor │ ──▶ │ ElementResolver │ ──▶ │ ActionExecutor │ │ DOM → JSON tree │ │ JSON + instr │ │ fallback chain: │ │ (strip noise, │ │ → LLM → │ │ testid→id→role→ │ │ cap tokens) │ │ {ref, action} │ │ text→bbox │ └─────────────────┘ └──────────────────┘ └───────────────────┘ │ fail? re-snapshot + retry ``` ## Install ```bash pip install -r requirements.txt playwright install chromium ``` *(PyPI package coming once the API stabilizes — for now, clone and install locally.)* ## Quickstart ```python from playwright.sync_api import sync_playwright from llm_browser_agent import BrowserAgent def llm_call(system_prompt, user_prompt): # plug in Groq, OpenAI, OpenRouter, local Ollama — anything that # takes (system, user) and returns text ... with sync_playwright() as p: browser = p.chromium.launch() page = browser.new_page() page.goto("https://example.com") agent = BrowserAgent(page, llm_call=llm_call) agent.do("click the 'Sign up' button") ``` See [`examples/basic_usage.py`](examples/basic_usage.py) for a full working example against a live demo site using Groq's Llama 3.3. ## Safety Actions the resolver flags as `destructive` (delete, submit payment, confirm-irreversible) are **blocked by default** unless you supply a `confirm_callback`: ```python agent = BrowserAgent(page, llm_call=llm_call, confirm_callback=lambda action: input(f"Confirm: {action.rationale}? (y/n) ") == "y") ``` ## Project status Early alpha. The core compress → resolve → execute → fallback loop works end-to-end (see [`examples/basic_usage.py`](examples/basic_usage.py)) but there's real work ahead: - [ ] Shadow DOM / iframe traversal - [ ] Multi-step instruction planning ("check out with the cheapest item") - [ ] Caching resolved selectors per-page-hash to cut LLM calls on repeat runs - [ ] Accessibility-tree-based compression (vs current DOM walk) for better token efficiency - [ ] Visual grounding fallback (screenshot + coordinates) for canvas-heavy UIs - [ ] Async/Playwright-async-API support (currently sync only) ## Contributing Good first issues are tagged [`good first issue`](../../issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22). See [CONTRIBUTING.md](CONTRIBUTING.md). The hardest, highest-impact area right now is **DOM compression quality** (`llm_browser_agent/dom_compressor.py`) — better pruning heuristics directly reduce token cost and improve resolution accuracy. ## License MIT — see [LICENSE](LICENSE).