# Interaction Map System ## Overview The Interaction Map system provides reliable element targeting for browser automation by generating a structured JSON representation of all interactive elements on a webpage. This eliminates brittle CSS selectors and enables text-based element search with automatic selector generation. ## Architecture ### Components 1. **Map Generator** (`src/cdp/map/generate-interaction-map.ts`) - Browser-side script that extracts all interactive elements - Generates multiple selector types for each element - Handles SVG elements, disabled states, React components 2. **Map Manager** (`src/daemon/map-manager.ts`) - Daemon-level automatic map generation on page load - 10-minute cache with auto-regeneration - URL-based cache validation - Event-driven DOM stabilization detection 3. **Map Query Module** (`src/cdp/map/query-map.ts`) - Loads and queries interaction maps - Searches by text, type, ID, visibility - Returns best selector with alternatives 4. **CLI Integration** (`src/cli/commands/interaction.ts`) - Smart Mode options: `--text`, `--index`, `--type`, `--viewport-only` - Automatic map querying before action execution - Fallback to alternative selectors on failure ### Automatic Map Generation Maps are automatically generated when: - Navigating to a new page (`node .browser-pilot/bp navigate -u ""`) - Page reload (`node .browser-pilot/bp reload`) - Cache expires (10 minutes) - Manual force generation (daemon command) No manual map generation needed - the daemon handles it automatically. Output location: `.browser-pilot/interaction-map.json` ## JSON Structure Maps use a hybrid structure optimized for both direct access and search: ```json { "url": "https://example.com", "timestamp": "2025-11-05T14:39:03.598+09:00", "viewport": { "width": 2560, "height": 1305 }, "elements": { "elem_0": { "id": "elem_0", "type": "button", "tag": "button", "text": "Submit", "value": null, "selectors": { "byText": "//button[contains(text(), 'Submit')]", "byId": "#submit-btn", "byCSS": "button.btn.btn-primary", "byRole": "[role='button']", "byAriaLabel": "[aria-label='Submit form']" }, "attributes": { "id": "submit-btn", "class": "btn btn-primary", "disabled": false }, "position": { "x": 1275, "y": 650 }, "visibility": { "inViewport": true, "visible": true, "obscured": false }, "context": { "section": "Form" } } }, "indexes": { "byText": { "Submit": ["elem_0", "elem_15"], "Delete": ["elem_5", "elem_6", "elem_7"] }, "byType": { "button": ["elem_0", "elem_1", "elem_2"], "input-text": ["elem_10", "elem_11"] }, "inViewport": ["elem_0", "elem_1", "elem_2", "elem_10"] }, "statistics": { "total": 45, "byType": { "button": 12, "input-text": 5, "a": 8 }, "duplicates": 3 } } ``` ### Key Features **1. Key-Value Structure** (`elements`) - Direct ID access: `map.elements["elem_0"]` - Avoids array iteration for known IDs **2. Indexes** (fast lookup) - `byText`: Maps text content → element IDs - `byType`: Maps element types → element IDs - `inViewport`: Array of visible element IDs **3. Multiple Selectors** - `byText`: XPath with tag name (e.g., `//button[contains(text(), 'Submit')]`) - `byId`: CSS ID selector (highest priority) - `byCSS`: CSS class selector - `byRole`: ARIA role selector - `byAriaLabel`: ARIA label selector **4. Automatic Indexing** - Duplicate text elements get indexed: `(//button[contains(text(), 'Delete')])[2]` - Enables "click the 3rd Delete button" functionality **5. Auto-Caching** - 10-minute cache TTL - Automatically regenerates on expiration or navigation - URL-based validation to prevent stale maps ## Element Detection ### Interactive Element Types The map generator detects: - Standard inputs: ``, `