# Browser Automation Toolkit [![CI](https://github.com/AH64-dll/browser-automation-toolkit/actions/workflows/ci.yml/badge.svg)](https://github.com/AH64-dll/browser-automation-toolkit/actions/workflows/ci.yml) [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE) ![Python](https://img.shields.io/badge/python-3.9%2B-blue) Five small, documented Python utilities for watching, filling, scraping and health-checking web pages through a real Chromium browser. No Selenium, no WebDriver binaries, no accounts — the toolkit talks directly to any Chromium-based browser (Chrome, Edge, Brave, Chromium) over the built-in **Chrome DevTools Protocol**. One dependency (`websocket-client`); everything else is stdlib. ``` ┌────────────────────────────────────────────────────────────┐ │ your browser (started with --remote-debugging-port=9222) │ │ ▲ ▲ ▲ ▲ │ │ page_watcher form_filler table_scraper change_diff │ │ └────────── cdp_client ───────┘ │ │ cdp_healthcheck │ └────────────────────────────────────────────────────────────┘ ``` ## Why not Selenium? - **No driver downloads.** No chromedriver version roulette, ever. - **No accounts, no cloud.** It drives the browser already on your machine. - **Tiny surface.** ~200 lines of reusable CDP client instead of a framework. - **You keep control.** Plain functions and CLI entry points, not magic objects. ## What's inside | Utility | What it does | Typical use | |---|---|---| | `page_watcher` | Polls a page (or one CSS element) and alerts on change — console, JSONL log, or your webhook | price/stock/status monitoring | | `form_filler` | Fills & optionally submits forms from a declarative JSON spec; dry-run by default | replaying repetitive form entries safely | | `table_scraper` | Extracts HTML tables (incl. JS-rendered) to CSV/JSON, rowspan/colspan handled | turning report pages into data files | | `change_diff` | Snapshots page text over time into a local store and prints unified diffs | "what changed on this page since yesterday?" | | `cdp_healthcheck` | One-shot health report for debugging-enabled browsers: version, latency, tab leaks, attach test | pre-flight check before long automation runs | Plus `batkit/cdp_client.py` — a ~200-line dependency-light CDP client you can reuse in your own scripts and learn real CDP from. ## Requirements - Python 3.9+ - Any Chromium-based browser - `pip install -r requirements.txt` (only `websocket-client`; everything else is stdlib) ## Quick start **1. Start a browser with a debugging port** (a throwaway profile keeps your main profile untouched): ```bash google-chrome --remote-debugging-port=9222 --user-data-dir=/tmp/chrome-automation # or: chromium --remote-debugging-port=9222 --user-data-dir=/tmp/chrome-automation # or: microsoft-edge --remote-debugging-port=9222 --user-data-dir=/tmp/chrome-automation ``` **2. Check it's healthy:** ```bash python -m batkit.cdp_healthcheck # [127.0.0.1:9222] HEALTHY # browser : Chrome/126.0.0.0 # latency : 3.2 ms # tabs : 1 (0 blank/new-tab) # attach+eval : ok ``` **3. Watch a page for changes:** ```bash python -m batkit.page_watcher https://example.com --interval 60 --log changes.jsonl ``` **4. Scrape every table on a report page:** ```bash python -m batkit.table_scraper https://example.com/report -o table.csv python -m batkit.table_scraper https://example.com/report --table-index 2 -o table.json ``` **5. Snapshot + diff a page over time:** ```bash python -m batkit.change_diff snap https://example.com --store snaps/ python -m batkit.change_diff diff https://example.com --store snaps/ # continuous: python -m batkit.change_diff watch https://example.com --every 300 --store snaps/ ``` **6. Fill a form from a spec (dry run first!):** ```bash python -m batkit.form_filler examples/form-contact.json # dry run python -m batkit.form_filler examples/form-contact.json --submit # live run ``` ## Using the library from Python Every utility is also importable: ```python from batkit.cdp_client import CDPClient client = CDPClient(port=9222) print(client.version()["Browser"]) tab = client.new_tab("https://example.com") with client.attach(tab) as t: print(t.title()) t.screenshot("example.png") client.close_tab(tab["id"]) ``` ```python from batkit.page_watcher import fetch_state, content_fingerprint from batkit.change_diff import unified_diff diff = unified_diff("old text\n", "new text\n") ``` See [`examples/`](examples/) for runnable scripts covering each module: ``` examples/ watch_price.py poll one element and log changes to JSONL scrape_tables.py save all tables from a page as CSV files form-contact.json declarative form spec used by form_filler nightly_diff.sh cron-friendly snapshot+diff loop ``` Run examples from the toolkit root so `batkit` imports resolve: ```bash python examples/watch_price.py https://example.com "#main" 30 ``` ## Safety & good practice - `form_filler` **never submits unless you pass `--submit`.** Always dry-run first. - Point tools only at sites you're authorized to automate; respect each site's terms and robots.txt. Keep polling intervals polite (≥30–60 s). - Secrets stay out of specs: use `"env:MY_VAR"` value references instead of hard-coding anything sensitive. - Use a dedicated `--user-data-dir` browser profile for automation. - Everything runs locally. The toolkit has no telemetry and no network calls beyond the pages you point it at. ## Troubleshooting | Symptom | Fix | |---|---| | `cannot reach browser at http://127.0.0.1:9222` | Browser not started or port differs → run `python -m batkit.cdp_healthcheck --ports 9222 9223 ...` to find it | | `DevTools remote debugging requires a non-data URL` warning at launch | You passed `--remote-debugging-port` together with a data: URL; just open tabs normally | | Attach works but evaluate returns null | Page may be in a crashed state; restart the browser, then re-run `cdp_healthcheck` | | Table comes out empty | It's rendered late by JS — add a short sleep after navigation, or use `change_diff --browser` style flows after full load | ## License MIT — use it commercially, modify it, ship it. No warranty. See [LICENSE](LICENSE). ## Version history - **v1.0.0** — first release: page_watcher, form_filler, table_scraper, change_diff, cdp_healthcheck, shared cdp_client.