Skip to content

Locators

akib35 edited this page Nov 5, 2026 · 4 revisions

Locators

A locator is the string a page block uses to name an element: Playwright-like syntax (role=button[name="Save"], label=Email >> nth=0), resolved inside the page. Read this page to write or debug a locator, to change how one resolves, or to understand which locator the picker and recorder suggest and why.

Source: locatorSyntax.ts (parsing, shared with the exporter), locator.ts (resolution), aria.ts (roles and names), bestLocator.ts (generated readable locators), selector.ts (generated CSS).

Pipeline

"form#login >> role=button[name=\"Sign in\"]"
   │ parseLocatorSteps (core, string only)      → [{engine:'css', body:'form#login'}, {engine:'role', body:'button[name="Sign in"]'}]
   │ resolveLocator(source, root) (dom)         → step by step, each step searching inside the previous matches
   ▼
 Element[] in document order                    → the agent's one() (strict) or all()

Syntax (locatorSyntax.ts)

export class LocatorSyntaxError extends Error {}
export interface LocatorStep { engine: string; body: string }
export const ENGINES: Set<string>;   // css text role label placeholder testid alt title xpath nth frame
export function parseLocatorSteps(source: string): LocatorStep[];
  • Steps are split on >> outside quotes (" / ', with \ escapes) and outside [...] / (...). The shadow-piercing >>> is never a split point.
  • Each step is trimmed, then classified:
Step looks like Engine
<engine>=<body> with a known engine name (lowercase, optional spaces before =) that engine; body trimmed
starts with // or (// xpath
fully wrapped in "..." text (exact, body keeps the quotes)
anything else css
  • Empty locator → "The locator is empty"; an empty step (a >> >> b) → "Invalid locator: …".
  • An unknown foo=bar is treated as CSS, which then fails as invalid CSS.

Who parses: locator.ts parseLocator (rethrows as LocatorError) and the Playwright exporter playwright.ts, which maps steps to getByRole, getByLabel, frameLocator… (see Core Modules).

Engines (locator.ts)

Engine Example Matches Shadow DOM Hidden elements
css (default) #login button, css=form input[name=email] querySelectorAll. a >>> b runs b inside the open shadow roots of a's matches. only via >>> included
text text=Sign in, text="Sign in", text=/sign\s?in/i, "Sign in" Smallest elements whose text matches: an element is dropped when one of its children matches too. Text is textContent, or value for input[type=button|submit|reset]. Skips script, style, noscript, template, head, title, meta, link. yes included (text= does not check visibility)
role role=button[name="Submit"], role=heading[level=2], role=checkbox[checked] getRole(el) === role, plus options below yes excluded unless [include-hidden]
label label=Email Controls of matching <label>s (label.control, else first input, select, textarea, [contenteditable] inside), and any element whose aria-label or aria-labelledby text matches yes included
placeholder placeholder=you@example.com placeholder attribute through textMatcher yes included
alt / title alt=Logo, title=Close that attribute through textMatcher yes included
testid testid=login exact value of data-testid, data-test-id, data-test, data-cy or data-qa (quotes allowed, no regex) yes included
xpath xpath=//form//button, //form//button document.evaluate with the scope as context node, element nodes only no included
nth >> nth=0, >> nth=-1 One element of the matches so far by index; negative counts from the end; out of range → no match — —
frame frame=iframe#pay >> label=Card See Frames — —

Text matching (textMatcher(body, exact = false))

Shared by text, label, placeholder, alt, title, role names, and the agent's Wait For text. Whitespace is collapsed and trimmed on both sides before comparing.

Body Match
/pattern/flags RegExp.test (bad pattern → Invalid regular expression: …)
"quoted" or 'quoted' (or exact = true) equal, case-sensitive
anything else case-insensitive substring

Role options

role=<role> followed by any of these brackets, in any order:

Option Meaning
[name="…"] / [name=/re/i] / [name=…] accessible name. Unlike text=, a quoted name is still a case-insensitive substring; add [exact] for equality.
[exact] exact (case-sensitive) name match
[checked], [checked=false] checkedState (native checked, else aria-checked / aria-pressed)
[level=N] aria-level, else the hN number
[include-hidden] also match elements hidden from ARIA

Anything else is Unknown role option [x]. Use name, exact, checked, level. Leftover text after the role is Invalid role locator.

Roles and names (aria.ts)

A simplified HTML-AAM mapping and accessible name computation, enough to say "the Submit button".

export function getRole(element: Element): string | null;
export function accessibleName(element: Element): string;
export function isHiddenForAria(element: Element): boolean;
export function checkedState(element: Element): boolean | null;
Element Role
explicit role="a b" a (first token)
input button (button/submit/reset/image), checkbox, radio, slider (range), spinbutton (number), textbox (text/email/tel/url, default), searchbox (search); text-like with list → combobox; password, date, file… → null
a / area link only with href
select listbox if multiple or size > 1, else combobox
img presentation if alt="", else img
section region only when it has a name
others TAG_ROLES: button, textarea→textbox, h1–h6→heading, ul/ol/menu→list, li→listitem, nav, main, header→banner, footer→contentinfo, aside→complementary, form, dialog, table, tr→row, td→cell, th→columnheader, article, fieldset→group, hr→separator, progress→progressbar, output→status, p→paragraph

accessibleName order: aria-labelledby text (looked up in the element's root first, so it works in shadow trees) → aria-label → for form fields: button value (Submit/Reset defaults), image alt, <label>s, then title / placeholder → img/area alt/title → fieldset legend, table caption → content text for name-from-content roles (button, link, heading, cell, option, tab, menuitem*, checkbox, radio, switch, treeitem, tooltip, listitem, row…) → title.

isHiddenForAria: aria-hidden="true" or [hidden] ancestor, display:none on it or an ancestor, or visibility:hidden on itself.

Chaining

A >> B searches B inside each match of A, strictly inside (A's own match is skipped), then dedupes and sorts into document order. nth picks from the matches so far and scopes the following steps to that one element. nth as the first step yields nothing.

Frames

frame=<css> finds iframe/frame elements with CSS in the current scopes; following steps search their contentDocuments.

  • Same-origin: entered directly, the result is element(s) from the inner document.
  • Cross-origin (contentDocument is null or throws): FrameBoundaryError(src, rest) where rest is the remaining steps re-serialised as engine=body >> …. The agent answers {status: 'frame', src, rest} and the driver re-runs rest in that frame (see Page Agent).
  • If any matched frame is cross-origin the whole locator hands over, even when another matched frame is same-origin. Point frame= at one iframe.
  • Frames can nest: the agent inside the frame may hand over again.
export class LocatorError extends Error {}
export class FrameBoundaryError extends LocatorError { readonly src: string; readonly rest: string }
export function parseLocator(source: string): LocatorStep[];
export function resolveLocator(source: string, root: ParentNode = document): Element[];
export function textMatcher(body: string, exact?: boolean): (text: string) => boolean;
export function describeElement(element: Element): string;   // button#save.primary "Save changes"

Examples

Given

<form id="login">
  <label for="email">Email address</label><input id="email" type="email" placeholder="you@example.com">
  <label>Password <input id="pw" type="password"></label>
  <button id="go" data-testid="submit-btn">Sign <b>in</b></button>
  <button id="cancel" type="button">Cancel</button>
</form>
<div hidden><button id="ghost">Sign in</button></div>
<ul><li id="l1">One</li><li id="l2">Two</li><li id="l3">Three</li></ul>
Locator Result
#login button go, cancel (strict actions fail: 2 matches)
text=sign in go, ghost (hidden ones count for text=)
role=button[name="sign in"] go (ghost is hidden; quoted name still case-insensitive)
role=button[name="Sign"][exact] nothing
label=Email / label=password email / pw
placeholder=you@example email
testid=submit-btn go
#login >> role=button >> nth=1 cancel
li >> nth=-1 l3
xpath=//li[2] or //li[2] l2
my-widget >>> button button inside the open shadow root

More cases live in tests/dom/locator.test.ts.

Generated locators

Readable: locatorCandidates(element, max = 5) / bestLocator(element)

Used by the picker (all candidates), the recorder and Detect fields (bestLocator). Ideas are generated in this order, deduplicated, and kept only if resolveLocator(idea, ownerDocument) returns exactly this element:

# Idea When
1 testid=<value> any test-id attribute
2 label="<text>" form field (input/select/textarea) with a first <label> (or aria-label) ≤ 60 chars
3 placeholder="<text>" form field with a placeholder
4 role=<role>[name="<name>"] has role and name (not paragraph/presentation)
5 role=<role>[name="<name>"][exact] same, not a form field (useful when the substring form matches several)
6 text="<text>" not a form field; whole text ≤ 40 chars
7 alt="…", title="…" attribute ≤ 60 chars
8 <tag>[name="…"] (+ [value="…"] for radios) form field with name
last generateSelector(element) CSS always, unless already listed

At most max − 1 readable ideas are kept, then the CSS selector. bestLocator returns the first.

Structural CSS: generateSelector(element) (selector.ts)

export function generateSelector(element: Element): string;
export function isStableId(id: string): boolean;
export function isStableClass(name: string): boolean;
export function cssEscape(value: string): string;
export function countMatches(doc: Document, selector: string): number;

Walks up from the element, prepending one segment per ancestor (a > b > c), and stops as soon as the selector matches exactly the element. Per element:

  1. Stable id → #id (and an id segment never gets :nth-of-type).
  2. Else first of data-testid, data-test, data-qa, data-cy, name, aria-label with a value ≤ 60 chars → tag[attr="value"].
  3. Else tag + up to two stable classes.
  4. :nth-of-type(n) only when another sibling matches the same segment.

Stable id: starts with a letter, ≤ 41 chars of [\w-], no run of 3+ digits, not an 8+ char hex string, no CSS-in-JS prefix (css-, sc-, jsx-, emotion-, mui-…). Stable class: same idea, plus not a state class (active, open, selected, is-active, ng-star-inserted…) and not a random mixed-case+digit hash. Rationale: ARCHITECTURE.md: Element picker.

The picker's selector field is this CSS selector (stable when text changes, which Watch Element and Read Element want: their field has pickPrefers: 'stable'); other locator fields default to the first readable candidate.

Failure modes

Symptom Cause Fix
X matches N elements … add ">> nth=0" strict action, several matches narrow (form#a >> …, [exact]) or >> nth=0
Times out waiting for X: no element matches wrong page/frame, element not rendered, closed shadow root, XPath into shadow DOM check the frame; CSS needs >>> for shadow DOM
text= matches a hidden duplicate text= does not filter hidden elements use role=…[name=…] (skips hidden)
role= finds nothing for a password field no implicit role for type=password in this model label= or placeholder=
role=button[name="Save"] also matches "Save draft" quoted role names are substrings add [exact]
Invalid CSS selector: … from what looked like an engine engine name misspelled or uppercase (Text=) → parsed as CSS lowercase engine name
The element is inside a frame from another site cross-origin frame and the driver could not find it wait for the frame to load; check frame= points at one iframe
Generated CSS selector breaks after a deploy ancestors only had generated classes/ids, so it fell back to :nth-of-type chains prefer the readable candidate

See also

Clone this wiki locally