page.getLocatorsByAI(prompt) lets you describe what you want in natural language (e.g., “all product cards in the carousel”) and automatically returns stable locators.
Why not vanilla Playwright? Low-level
page.locator() requires precise selectors that break when the DOM changes. On the other extreme, fully agentic agent.act() is powerful but heavyweight for simple element selection. getLocatorsByAI() bridges the gap—giving you AI-assisted, self-healing locators that feel native to Playwright.Built-in Autofix: The Key Advantage
The biggest pain point with traditional Playwright locators is maintenance. When developers refactor the UI, selectors break and tests fail—even though the functionality still works. WithgetLocatorsByAI, your tests automatically adapt to DOM changes because they target semantic intent, not brittle selectors.
- Traditional Playwright
- With getLocatorsByAI
Why does this work?
getLocatorsByAI uses the page’s accessibility tree (aria snapshot) to understand elements semantically. As long as your UI remains accessible, the AI can locate elements by their meaning rather than their implementation details.Because aria snapshots are non-visual, prompts referencing colors, sizes, or positions (e.g., “the blue button” or “the large header”) won’t work. Describe elements by their role, label, or text content instead.Installation
Install the Stably Playwright test integration and import it in place of the Playwright test runner:getLocatorsByAI method is automatically available on all page objects.
You can also import types for TypeScript:
getLocatorsByAI
Use prompt-based locator generation to find elements without writing fragile selectors.Basic Usage
How It Works
getLocatorsByAI performs intelligent element location in three steps:
- Aria Snapshot: Captures the page’s accessibility tree to understand the semantic structure of the DOM.
- AI Analysis: Evaluates your prompt against the aria snapshot to identify matching elements.
- Locator Generation: Returns Playwright-native locators that can be used like any standard locator.
Method Signature
prompt- Natural language description of the element(s) to locateoptions- Optional configurationmodel- AI model to use (see Model Selection)
locator behaves exactly like Playwright’s native Locator object. Call .all() to get an array of individual locators for each matched element.
Common Use Cases
Locate Dynamic ListsModel Selection
You can specify which AI model to use. If not specified, the backend default is used.| Model | Provider | Characteristics |
|---|---|---|
google/gemini-3-flash-preview | Fast, efficient | |
google/gemini-3-pro-preview | Most capable | |
openai/o4-mini | OpenAI | Efficient reasoning |
Best Practices
One Locator Type Per Prompt
Use the reason Field
The reason field explains how the AI interpreted your prompt. Use it for debugging when locators don’t match as expected. It’s also attached to the test report.
Troubleshooting
No elements found The AI couldn’t locate elements matching your description. Refine your prompt to use semantic identifiers._snapshotForAI not being a function, you need to upgrade Playwright:
getLocatorsByAI requires Playwright v1.54.1 or higher.
When to Use
-
Use
getLocatorsByAIwhen:- You need to locate elements in dynamic lists or repeated structures
- Writing precise selectors is difficult due to complex or changing DOM
- You want self-healing locators that adapt to minor DOM changes
- You’re prototyping tests and want to describe elements naturally
-
Use standard Playwright locators when:
- You have stable, unique selectors (IDs, data-testid attributes)
- Performance is critical and AI overhead is not acceptable
- The element structure is simple and unlikely to change
References
- Stably Playwright SDK:
@stablyai/playwright-test - Playwright Locators: Playwright Locator API
- Playwright Strictness: Locator Strictness