Overview
Agent Browser supports multiple selector strategies for finding and interacting with elements:- Refs (
@e1) - Recommended for AI agents - CSS Selectors (
#id,.class) - Traditional web selectors - Semantic Locators (
find role button) - Human-readable element queries - Text Selectors (
text=Submit) - Find by visible text - XPath (
xpath=//button) - XML path queries
Refs (Recommended for AI)
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:How Refs Resolve
Refs are mapped to Playwright locators:Ref Scoping
Refs are scoped to a single snapshot. After navigation or page changes, get a fresh snapshot: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:
See the ARIA roles spec for the full list.
Text Locators
Find by visible text content:--exact for strict matching (no substring matches).
Label Locators
Find inputs by their associated label:<label for="email">Email</label><input id="email"><label>Email <input></label>aria-label="Email"attributesaria-labelledbyreferences
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 bydata-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:- Ref:
@e1,ref=e1,e1(if matches/^e\d+$/) - Explicit prefix:
text=,xpath= - 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:--force:
Detached Elements
Playwright waits for elements to be attached to the DOM: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:Performance Comparison
Debugging Selectors
Highlight Elements
Highlight an element to verify your selector:Count Matches
Check how many elements match a selector:Snapshot Preview
Use snapshots to see what elements are available:Advanced Techniques
Shadow DOM
Penetrate shadow DOM boundaries:Iframes
Switch to iframe before selecting:Dynamic Content
Wait for elements to appear: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