Agentic Browser

AI agent for web automation and scraping, designed to plan, execute, and verify actions across any website

DOM manipulation and element targeting

Introduction#

How we find and use page elements: selectors, forms, inputs, interactive widgets, visibility, polling, and timeouts on slow pages.

Project structure#

The DOM manipulation and element targeting capabilities are implemented across:

  • Background service worker: orchestrates tab-level actions, executes injected scripts, and manages timeouts.
  • Content script: performs lightweight actions directly on the active page and recognizes common interactive patterns.
  • Utilities: capture DOM context, build interactive element lists, and route actions to the active tab.
  • Python service: formats DOM context for LLM-driven action planning.

Core components#

  • Selector-based element targeting: CSS selectors are used to locate elements for click, type, and visibility checks.
  • Form and input detection: heuristics identify inputs, textareas, selects, and contenteditable regions; appropriate events are dispatched to simulate user input.
  • Interactive component recognition: a curated set of selectors targets buttons, links, and role-based controls.
  • Visibility and state detection: computed styles and offset checks determine visibility; wait conditions support dynamic content.
  • Dynamic content handling: polling loops with timeouts ensure reliability on slow-loading pages.
  • Accessibility-aware attributes: aria-label and other attributes are captured to improve targeting accuracy.

Architecture overview#

The system uses a message-driven architecture:

  • Background script receives high-level commands and injects page scripts to perform DOM operations.
  • Content script handles simple actions directly on the page.
  • Utilities capture DOM context and route actions to the active tab.
  • Python service formats DOM context for LLM-driven action planning.

Detailed component analysis#

Selector-Based element targeting#

  • CSS selectors are passed to the page context for querying elements.
  • Actions include clicking elements identified by a selector and typing into input/select/textarea/contenteditable elements.
  • Visibility filtering supports returning only visible elements when listing candidates.

Form element detection and input identification#

  • Heuristics identify interactive elements: anchors, buttons, inputs, selects, textareas, and role-based buttons.
  • For contenteditable areas, innerText/textContent is set and input/change events are dispatched.
  • For standard inputs/textareas, value is set and input/change/keyboard events are dispatched.
  • A convenience action fills multiple fields and optionally submits the form.

Interactive component recognition#

  • Buttons and links are targeted broadly using selectors for anchor tags, buttons, and elements with role=“button”.
  • Content script includes a simple parser to match user intent to visible buttons by text matching.

Element state detection and visibility checks#

  • Visibility is determined by computed styles (display, visibility) and offsetParent presence.
  • A wait loop polls until an element exists or meets a visibility condition within a timeout.

Dynamic content handling and timeout strategies#

  • Wait actions use a configurable timeout and polling interval to accommodate slow-rendering pages.
  • Navigation and tab operations include completion waits and fallback timeouts.

Accessibility considerations and attribute capture#

  • Captured attributes include id, class, type, placeholder, name, aria-label, and visible text to improve reliability and accessibility.
  • The interactive list is limited to avoid excessive payloads.

Dependency analysis#

  • Background script depends on page injection to perform DOM operations safely.
  • Content script provides lightweight actions and pattern-based button matching.
  • Utilities coordinate DOM capture and action routing to the active tab.
  • Python service consumes DOM context to generate structured action plans.

Performance considerations#

  • Prefer specific CSS selectors to reduce query overhead.
  • Limit the number of returned interactive elements to avoid large payloads.
  • Use visibility filters when listing candidates to minimize false positives.
  • Batch operations and introduce small delays between actions to prevent overwhelming the page.

Troubleshooting guide#

Common issues and resolutions:

  • Element not found
  • Verify the selector specificity and scope.
  • Ensure the element exists before targeting; use wait conditions for dynamic content.
  • Confirm the page is fully loaded or use navigation waits.
  • Visibility errors
  • Use visibility checks or filter visible elements when listing candidates.
  • Adjust selectors to target visible subtrees.
  • Dynamic content timing
  • Increase timeout values for wait actions.
  • Use polling intervals suited to the page’s rendering speed.
  • Contenteditable vs. inputs
  • For contenteditable, set innerText/textContent and dispatch input/change events.
  • For inputs/textareas, set value and dispatch input/change/keyboard events.
  • Accessibility attributes
  • Include aria-label/id/class/name in selectors to improve reliability.
  • Mixed environments
  • For SPA or framework-heavy pages, ensure selectors target stable attributes and avoid brittle text-based targeting.

Conclusion#

Prefer stable selectors, capture useful attributes (id, name, aria-label, visible text), and poll with timeouts instead of single-shot queries on dynamic pages.