--- title: "HTML Snapshot — Static DOM Extraction, Inspection & X-SQL Querying" description: "Reference for htmlsnapshot commands (capture, get, query, summary, export, grep, inspect). Extract structured data from the raw HTML DOM via CSS selectors and X-SQL queries." tier: catalog --- # HTML Snapshot — Static DOM Extraction, Inspection & X-SQL Querying The `htmlsnapshot` family operates on a **static HTML snapshot** — the raw HTML of the current page parsed into a queryable DOM. Unlike interactive `snapshot` (accessibility-tree refs for `click`/`type`/`fill`), `htmlsnapshot` extracts structured data via CSS selectors and X-SQL queries. ## Comparison: snapshot vs htmlsnapshot | Feature | `snapshot` | `htmlsnapshot` | |---|---|---| | Data source | Accessibility tree | Raw HTML DOM | | Element addressing | Refs (`e5`) | CSS selectors only | | X-SQL support | No | Yes (`query`) | | Interactive element list | No | Yes (`htmlsnapshot` capture returns interactiveElements) | | Selector discovery | No | Yes (`inspect`) | | Output | YAML accessibility tree | HTML (`export`), structured data (`get`/`query`/`inspect`) | ## Commands ```bash browser4-cli htmlsnapshot # capture fresh static HTML snapshot + metadata browser4-cli htmlsnapshot get [selector] [name] [--page N] [--page-size N] [--all] # extract text/html/attr via CSS; html paginated at 2K lines, text not paginated browser4-cli htmlsnapshot query [url] --sql # X-SQL query against DOM (url defaults to current page) browser4-cli htmlsnapshot summary # compressed page summary (WPSI) browser4-cli htmlsnapshot export [--file ] [--clean] # save snapshot HTML to file browser4-cli htmlsnapshot get all [selector] [name] [--offset N] [--limit N] [--page N] [--page-size N] [--all] # extract ALL matches; html paginated at 2K lines, text not paginated browser4-cli htmlsnapshot grep [OPTIONS] [--page N] [--page-size N] [--all] # search snapshot HTML with regex; paginated by default (2K lines) browser4-cli htmlsnapshot inspect [selector] [--max N] [--depth D] # analyze DOM structure, suggest CSS selectors ``` `htmlsnapshot` (capture) always fetches a fresh snapshot, caches it, and returns enriched metadata including image/link counts and a list of interactive elements (with tag, class, id, aria attributes, and bounding box). Subsequent `get`/`query`/`export`/`inspect` reuse the cache until the next capture or page navigation. > **Note:** `htmlsnapshot get` looks up the page using the browser's current URL (after any redirects/navigations), so it works correctly on search-results pages and post-form-submission pages. ## Get — Extract data via CSS selectors Only CSS selectors are accepted — element refs (`e5`) are rejected. ```bash # First match only (querySelector semantics) browser4-cli htmlsnapshot get [name] # All matches (querySelectorAll semantics) browser4-cli htmlsnapshot get all [name] [--offset N] [--limit N] ``` | Field | Description | Requires `name`? | |---|---|---| | `text` | Visible text of matched element(s) | No | | `html` | Inner HTML of matched element(s) | No | | `attr` | Value of a named attribute | **Yes** (3rd argument) | **`get` returns only the first match.** For multiple results, use `htmlsnapshot get all` (returns a JSON array) or `htmlsnapshot query`. > **Warning:** Correlating multiple fields: Each `get all` call scans the whole document independently — running `get all text ".title"` and `get all text ".price"` produces two unaligned arrays (different lengths, different order). To extract correlated fields (title + price + URL per item), use `htmlsnapshot query` with X-SQL's `DOM_LOAD_AND_SELECT` scoped to a parent container. See the [list-page scraping pattern](x-sql-dom-load-select.md#dom_load_and_select). ### `get` (single) ```bash browser4-cli htmlsnapshot get text ".product-title" browser4-cli htmlsnapshot get attr ".product-image" data-src ``` ### `get all` (multiple) Returns a JSON array of strings. Supports `--offset` (skip first N) and `--limit` (max results). ```bash browser4-cli htmlsnapshot get all text "h2 a" # all product titles browser4-cli htmlsnapshot get all attr ".product-image" src # all image URLs browser4-cli htmlsnapshot get all text ".result" --limit 5 # first 5 results browser4-cli htmlsnapshot get all text ".result" --offset 10 # skip first 10 ``` ### Troubleshooting empty results If `htmlsnapshot get` returns an empty string when the page clearly has matching elements: 1. **Run `htmlsnapshot` first to capture a fresh snapshot:** `browser4-cli htmlsnapshot` then retry `get` 2. **Verify the CSS selector** with `htmlsnapshot grep ` to search the raw HTML 3. **Use `htmlsnapshot query` or `htmlsnapshot get all`** for multiple results or complex queries 4. **Check page load:** ensure the page finished loading (AJAX content may take time) ## Query — X-SQL against HTML snapshot The `--sql` flag is **required**. Use `@url` as a placeholder for the target URL. X-SQL uses the **H2 database** SQL dialect with DOM UDFs. Only simple `SELECT ... FROM DOM_LOAD_AND_SELECT(url, cssQuery)` queries are supported — no CTEs, subqueries, `EXPLODE`, or joins. > **Important:** `@url` must appear **unquoted** in SQL. `SQLTemplate.createSQL(url)` handles escaping internally. > - ✅ `FROM DOM_LOAD_AND_SELECT(@url, ':root')` > - ❌ `FROM DOM_LOAD_AND_SELECT('@url', ':root')` > - ❌ `FROM DOM_LOAD_AND_SELECT('.', ':root')` — the literal `'.'` is not a valid URL. Use the `@url` placeholder to reference the current page. ### Three ways to provide the SQL query **1. File (recommended — no shell escaping issues):** Prefix the `--sql` value with `@` to read from a `.sql` file: ```bash # Write query to file (no escaping needed) cat > query.sql << 'SQLEOF' SELECT DOM_BASE_URI(dom) AS url, DOM_FIRST_TEXT(dom, '#productTitle') AS title FROM DOM_LOAD_AND_SELECT(@url, 'body') WHERE DOM_FIRST_TEXT(dom, '#productTitle') != 'Sponsored' SQLEOF # Run it browser4-cli htmlsnapshot query "https://www.amazon.com/dp/B08PP5MSVB" --sql @query.sql ``` > **Note:** X-SQL function names are case-insensitive. `DOM_FIRST_TEXT` and `dom_first_text` are equivalent. This reference uses UPPERCASE for clarity. **2. Stdin (for piped/scripted workflows — also avoids quoting):** ```bash cat query.sql | browser4-cli htmlsnapshot query --sql-stdin # or browser4-cli htmlsnapshot query --sql-stdin < query.sql # with a URL browser4-cli htmlsnapshot query "https://example.com" --sql-stdin < query.sql ``` **3. Base64 (transport-safe — no quoting, works across all platforms):** ```bash # Encode once, pass anywhere without escaping base64 -w0 query.sql > query.b64 browser4-cli htmlsnapshot query "https://example.com" --sql @query.b64 --sql-base64 # Or inline the base64 value directly browser4-cli htmlsnapshot query "https://example.com" --sql "$(base64 -w0 query.sql)" --sql-base64 ``` **4. Inline (requires careful shell escaping on Windows):** ```bash # Simple queries without quotes in selectors work inline: browser4-cli htmlsnapshot query --sql " SELECT DOM_BASE_URI(dom) AS url, DOM_FIRST_TEXT(dom, 'h1') AS title FROM DOM_LOAD_AND_SELECT(@url, 'body'); " # Queries with quoted selectors or != require escaping — prefer @file, --sql-stdin, or --sql-base64 ``` To control caching or rendering, append load options to the URL (e.g. `https://example.com/page -i 1d -njr 3`). ## Summary — Web Page Summary Index (WPSI) Generates a deterministic, AI-readable compressed page summary (typically <1% of original HTML) as a YAML file. Includes page metadata, structure landmarks, key content nodes with CSS selector hints, list/table detection, and stats. Requires a previously captured HTML snapshot. ```bash browser4-cli htmlsnapshot summary ``` ## Export Save full snapshot HTML to a local file. The exported HTML is pretty-formatted for direct use with tools like `grep`. Use `--clean` to produce a minimal HTML file suitable for LLM consumption — strips `