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:| Role | Element Examples |
|---|---|
button | <button>, <input type="button">, role="button" |
link | <a href="..."> |
textbox | <input type="text">, <textarea> |
checkbox | <input type="checkbox"> |
radio | <input type="radio"> |
combobox | <select>, ARIA comboboxes |
heading | <h1>, <h2>, etc. |
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:| Action | Description | Example |
|---|---|---|
click | Click element | find role button click |
fill | Fill input | find label "Email" fill "user@example.com" |
type | Type into input | find label "Search" type "query" |
hover | Hover element | find role link hover |
focus | Focus element | find role textbox focus |
check | Check checkbox | find role checkbox check |
uncheck | Uncheck checkbox | find role checkbox uncheck |
text | Get text content | find role heading text |
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
| Selector Type | Speed | Stability | AI-Friendly |
|---|---|---|---|
| Refs | ⚡⚡⚡ (cached) | ⭐⭐⭐ | ⭐⭐⭐ |
| CSS (ID) | ⚡⚡⚡ | ⭐⭐ | ⭐ |
| CSS (class) | ⚡⚡⚡ | ⭐ | ⭐ |
| CSS (data-testid) | ⚡⚡⚡ | ⭐⭐⭐ | ⭐⭐ |
| Semantic (role) | ⚡⚡ | ⭐⭐⭐ | ⭐⭐ |
| Text | ⚡⚡ | ⭐⭐ | ⭐⭐ |
| XPath | ⚡ | ⭐ | ⭐ |
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