Skip to main content

Overview

Agent Browser supports multiple selector strategies for finding and interacting with elements:
  1. Refs (@e1) - Recommended for AI agents
  2. CSS Selectors (#id, .class) - Traditional web selectors
  3. Semantic Locators (find role button) - Human-readable element queries
  4. Text Selectors (text=Submit) - Find by visible text
  5. XPath (xpath=//button) - XML path queries
Each has different tradeoffs for stability, readability, and performance.

Overview

Refs provide deterministic element selection from snapshots:

Why Use Refs?

Deterministic: Points to exact element from snapshot
Fast: No DOM re-query needed
AI-Friendly: Easy for LLMs to generate
Accessible: Based on ARIA tree (screen reader compatible)

Ref Formats

Three equivalent formats:
All three parse to the same reference:

How Refs Resolve

Refs are mapped to Playwright locators:
Example:

Ref Scoping

Refs are scoped to a single snapshot. After navigation or page changes, get a fresh snapshot:
Using stale refs may fail or interact with the wrong element. See Snapshot Refs for details.

CSS Selectors

Basic CSS

Standard CSS selector syntax:

When to Use CSS

Stable IDs: When elements have unique, stable IDs
Test IDs: When using data-testid attributes
Simple queries: For one-off scripts
Dynamic classes: Avoid if classes change frequently
AI workflows: Hard for LLMs to generate correctly

Performance

CSS selectors are fast (direct DOM query), but may be brittle:

Semantic Locators

Overview

Find elements by their semantic meaning instead of DOM structure:

Role Locators

Find by ARIA role and optional name:
Supported roles: See the ARIA roles spec for the full list.

Text Locators

Find by visible text content:
Text matching is case-sensitive by default. Use --exact for strict matching (no substring matches).

Label Locators

Find inputs by their associated label:
This works for:
  • <label for="email">Email</label><input id="email">
  • <label>Email <input></label>
  • aria-label="Email" attributes
  • aria-labelledby references

Placeholder Locators

Find inputs by placeholder text:

Alt Text Locators

Find images by alt text:

Title Locators

Find elements by title attribute:

Test ID Locators

Find by data-testid attribute:

Positional Locators

Select specific instances when multiple elements match:

Actions

Semantic locators support these actions:

When to Use Semantic Locators

Human-readable: Easy to understand what they select
Stable: Less affected by DOM changes
Accessible: Based on ARIA/semantic HTML
Verbose: Longer than refs
Slower: Requires DOM query on each use

Text Selectors

Exact Text Match

Substring Match

Case-Insensitive

XPath Selectors

Basic XPath

When to Use XPath

Complex queries: When CSS can’t express the logic
Text-based selection: XPath has better text functions
Readability: Hard to read and maintain
Performance: Generally slower than CSS

Selector Precedence

When a selector could match multiple strategies, Agent Browser checks in this order:
  1. Ref: @e1, ref=e1, e1 (if matches /^e\d+$/)
  2. Explicit prefix: text=, xpath=
  3. CSS: Anything else

Selector Composition

Combine selectors for more precise targeting:

CSS + Pseudo-Selectors

Chaining Find Commands

Special Selectors

Visible Elements Only

Playwright automatically filters to visible elements:
To include hidden elements, use --force:

Detached Elements

Playwright waits for elements to be attached to the DOM:
Timeout is 25 seconds by default (configurable via AGENT_BROWSER_DEFAULT_TIMEOUT).

Selector Best Practices

For AI Agents

Use refs:

For Manual Scripting

Use semantic locators or stable CSS:

For Testing

Use data-testid attributes:
Test IDs are stable across UI changes.

Performance Comparison

Debugging Selectors

Highlight Elements

Highlight an element to verify your selector:
The element will be outlined in red in the browser.

Count Matches

Check how many elements match a selector:
If count > 1, your selector is ambiguous and may click the wrong element.

Snapshot Preview

Use snapshots to see what elements are available:
This shows all interactive elements with their refs and roles.

Advanced Techniques

Shadow DOM

Penetrate shadow DOM boundaries:

Iframes

Switch to iframe before selecting:

Dynamic Content

Wait for elements to appear:
Or use find which auto-waits:

Next Steps

  • Architecture - Understand the Rust CLI + Node.js daemon design
  • Snapshot Refs - Deep dive into the ref system
  • Sessions - Learn about session isolation and persistence