Skip to main content
Writing reliable locators is often painful—users must inspect HTML, guess selectors, and tweak through trial and error. This becomes especially hard when dealing with dynamic lists or repeating elements. 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. With getLocatorsByAI, your tests automatically adapt to DOM changes because they target semantic intent, not brittle selectors.
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:
The 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:
  1. Aria Snapshot: Captures the page’s accessibility tree to understand the semantic structure of the DOM.
  2. AI Analysis: Evaluates your prompt against the aria snapshot to identify matching elements.
  3. Locator Generation: Returns Playwright-native locators that can be used like any standard locator.

Method Signature

Parameters:
  • prompt - Natural language description of the element(s) to locate
  • options - Optional configuration
Types:
The returned 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 Lists
Find Specific Elements by Role
Target Elements by Position

Model Selection

You can specify which AI model to use. If not specified, the backend default is used.

Best Practices

One Locator Type Per Prompt

While you can request multiple unrelated locators in a single prompt (e.g., “get all the circle buttons and the first two headers”), this is discouraged. Playwright’s strictness model recommends locators that uniquely identify target elements. Mixing unrelated elements in one locator can lead to unexpected behavior when the page changes.

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.
Too many elements matched The prompt is matching more elements than intended. Add constraints to narrow the selection.
Playwright version error If you see an error about _snapshotForAI not being a function, you need to upgrade Playwright:
getLocatorsByAI requires Playwright v1.54.1 or higher.

When to Use

  • Use getLocatorsByAI when:
    • 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