Repository navigation
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).
"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()
export class LocatorSyntaxError extends Error {}
export interface LocatorStep { engine: string; body: string }
export const FILTERS: Set<string>; // has-text has-not-text has has-not visible
export const ENGINES: Set<string>; // css text role label placeholder testid alt title xpath nth frame + FILTERS
export const ROLE_OPTIONS; // name exact checked pressed expanded selected disabled level include-hidden
export function splitAlternatives(source: string): string[]; // `A || B` → ['A', 'B']
export function parseLocatorSteps(source: string): LocatorStep[]; // one alternative
export function parseLocatorAlternatives(source: string): LocatorStep[][];
export function nestedLocator(body: string): string; // has=/has-not= body, unquoted
export function visibleFilter(body: string): boolean; // '', true → true; false → false
export function parseRoleSelector(body: string): { role: string; options: { key; value? }[] };
export function roleState(option): boolean; // [x] / [x=true] / [x=false]
export function unquoteValue(value: string): string;-
Alternatives are split first, on
||outside quotes and brackets (a lone|, XPath's union, is left alone).||binds loosest:A >> B || Cis(A >> B) || C. An empty side →Invalid locator: … ("||" needs a locator on both sides). -
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 letters and -, optional spaces before =) |
that engine; body trimmed |
exactly visible
|
the visible filter, body true
|
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=baris treated as CSS, which then fails as invalid CSS.
Who parses: locator.ts (resolveLocator, parseLocator; syntax errors
rethrown as LocatorError), the checker check.ts locatorProblem (filters as a
first step, bad visible=, unknown or mistyped role options and state values, a bare step that looks like
a mistyped visible, and the inner locator of has= checked recursively), and the Playwright exporter
playwright.ts, which maps steps to getByRole, getByLabel,
frameLocator, .filter({ hasText | hasNotText | has | hasNot | visible }) and .or() (see
Core Modules). A quoted has-text="x" exports as hasText: /^x$/ (whole text).
| 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 | — | — |
Filter steps narrow the matches so far instead of searching inside them (Playwright's locator.filter()),
so the following steps search inside the kept elements. A filter as the first step is a LocatorError
(… filters the matches of the step before it).
| Filter | Keeps a match when |
|---|---|
has-text=<text> |
its text (textContent, or value for input buttons) passes textMatcher(body): substring any case, "exact" (whole text), /regex/
|
has-not-text=<text> |
it does not |
has=<locator> |
resolveLocator(inner, match) finds an element other than the match itself (strictly inside). The body is unquoted when fully quoted, so an inner locator with >> or ` |
has-not=<locator> |
it does not |
visible, visible=true / visible=false
|
isVisibleElement(match) is true / false: connected, no [hidden] / display:none on it or an ancestor, no visibility:hidden|collapse on itself, and a non-empty box when the document has layout (same rule as the agent's isVisible) |
A || B resolves each alternative from the same root and merges the matches in document order, without
duplicates. A FrameBoundaryError from one alternative is thrown only when no alternative found anything.
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=<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) |
[pressed], [pressed=false]
|
pressedState: aria-pressed
|
[expanded], [expanded=false]
|
expandedState: aria-expanded
|
[selected], [selected=false]
|
selectedState: a native <option>'s selected, else aria-selected
|
[disabled], [disabled=false]
|
disabledState: native disabled on button/input/select/textarea/option/optgroup/fieldset, a disabled fieldset ancestor (outside its first legend) or optgroup, or aria-disabled="true" on it or an ancestor |
[level=N] |
aria-level, else the hN number |
[include-hidden] |
also match elements hidden from ARIA |
State options take true / false (quotes allowed); a bare [x] is true. An element whose state
reader answers null (no aria-pressed / aria-expanded / aria-selected) matches neither [x] nor
[x=false], as in Playwright. Parsing is shared (parseRoleSelector in core): an unknown option is
Unknown role option [x]. Use name, exact, checked, pressed, expanded, selected, disabled, level, include-hidden.,
a bad state value [pressed=…] takes true or false, a non-number level [level=…] takes a number.
Leftover text after the role is Invalid role locator.
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;
export function pressedState(element: Element): boolean | null;
export function expandedState(element: Element): boolean | null;
export function selectedState(element: Element): boolean | null;
export function disabledState(element: Element): boolean;
export function accessibleDescription(element: Element): string; // aria-describedby, aria-description, title (if not the name)| 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.
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.
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 (
contentDocumentis null or throws): skipped, but remembered asFrameBoundaryError(src, rest, frame)whererestis the remaining steps re-serialised asengine=body >> …. - Every matched same-origin frame is searched. Only when the whole locator finds nothing and a
cross-origin frame was skipped is that error thrown (for the first such frame): the agent answers
{status: 'frame', src, rest}and the driver re-runsrestin that frame (see Page Agent). Soframe=iframe >> text=Payfinds the button in whichever same-origin frame has it, and falls back to a cross-origin frame. An element that has not appeared yet in a same-origin frame can therefore be looked for in a cross-origin one meanwhile; pointframe=at one iframe when that matters. - 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; readonly frame?: Element }
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"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 |
|
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 and, for filters, || and role states, tests/dom/locatorFilters.test.ts.
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.
Scoped ideas (has-text). When none of the ideas above is unique (the same "Delete" button in every
row), scopedIdeas tries the first two role/text/label/placeholder ideas inside the nearest ancestors (up
to 8 levels) whose role is listitem, row, article, group, region, form, dialog, navigation or cell, picked
by that ancestor's first short text outside the element: role=listitem >> has-text=Banana >> role=button[name="Delete"]. Only texts that are safe unquoted (≤ 40 chars, no quotes, /, >, |, [,
], \, =) are used, since an unquoted has-text is a substring match. Kept only if unique, like the rest.
firstLocator(element, cache?) gives the same answer as bestLocator but stops at the first idea that
works; with a cache (locator → matches, one Map per document, only while the page does not change)
each idea is resolved once for all elements. Page snapshots use it for every element they list.
role= checks the name before the hidden state: isHiddenForAria reads computed styles up the tree,
the most expensive test, so it only runs for elements that otherwise match.
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:
- Stable id →
#id(and an id segment never gets:nth-of-type). - Else first of
data-testid,data-test,data-qa,data-cy,name,aria-labelwith a value ≤ 60 chars →tag[attr="value"]. - Else
tag+ up to two stable classes. -
:nth-of-type(n)only when another sibling matches the same segment.
Inside an open shadow root the walk stops at the root (uniqueness is checked with the root's
querySelectorAll) and the host's own selector is put in front: sign-up >>> #join. countMatches
resolves such selectors through resolveLocator. Elements in iframes get their frame=<iframe css> >>
prefix from the picker/recorder content scripts (Picker and Recorder: Frames).
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.
| 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 |
- Page Agent: strictness and waits around resolution
- Picker and Recorder: where generated locators come from
- Page Protocol: requests that carry locators
-
Core Modules:
locatorSyntax.ts,playwright.ts - ARCHITECTURE.md: Page blocks and the page agent
Generated from docs/wiki in the repo by npm run wiki:publish. Edit the repo files, not this wiki.
Using BlockFlow
- What is BlockFlow
- Installing
- Getting Started
- Concepts
- Using the Editor
- Workflows as Scripts
- Recipes
- Capabilities and Limitations
- Troubleshooting
- Roadmap
Reference
Internals: engine
Internals: browser
Outside access (M4)
Internals: page and editor
Project