skip to content

Locators and Auto-Waiting

How you name an element in the page, how the framework decides it is ready, and the call that finally acts on it. Interviewers start here because the locator model is Playwright's core idea.

on this pageshow

explore

questions

page 2 of 2

When is Playwright's locator.waitFor() still the right call rather than a retrying assertion?

level: middleimportance: should knowfreq 48%

basics

~20 s

Use locator.waitFor() when an element must reach a state but there is nothing to assert - a precondition, or before a raw read. Where the test would assert, a retrying matcher is better: it reports the actual value.

open as a page

In Playwright's page.getByRole, when can you use the checked, pressed, expanded, selected and level options?

level: middleimportance: should knowfreq 51%

basics

~20 s

Only on roles that support the state. Playwright reads the value from an aria attribute or the native control and rejects an unsupported pairing, such as pressed on anything but button, with an error naming the allowed roles.

open as a page

In a Playwright CSS selector, what is the difference between :has() and :has-text()?

level: middleimportance: should knowfreq 52%

basics

~20 s

The :has() pseudo-class takes a selector and keeps elements that contain a match for it. The :has-text() pseudo-class takes a string and keeps elements whose subtree contains that text, matched case-insensitively as a trimmed substring.

open as a page

In Playwright, how do you restrict page.locator('button') to only the visible buttons?

level: middleimportance: should knowfreq 44%

basics

~20 s

Call locator.visible(), added in Playwright 1.63, which returns a locator matching only the visible elements. The older :visible CSS pseudo-class does the same inside a selector string. Visible means a non-empty bounding box and no visibility:hidden.

open as a page

In a Playwright test, when an issue title sits inside a card, which elements does page.getByText('Fix login redirect') match?

level: middleimportance: should knowfreq 55%

basics

~20 s

Only the innermost element whose own text matches. Playwright drops any element that has a child element also matching, so the card and its wrapper are excluded and the span holding the title is returned.

open as a page

In Playwright, how do you attach a file when the page renders no file input to target?

level: seniorimportance: should knowfreq 46%

basics

~10 s

Wait for the page's filechooser event. Start waiting before clicking the control, then call setFiles on the FileChooser Playwright hands you; this works even when script creates the input and discards it.

open as a page

In Playwright, why might a comment box's @mention autocomplete never open when the test uses locator.fill()?

level: seniorimportance: should knowfreq 41%

basics

~20 s

fill inserts the whole string in one step and dispatches only an input event, never keydown or keyup. A menu that opens on key handlers therefore never sees a trigger. Type the trigger characters with pressSequentially instead.

open as a page

In Playwright, what does adding force: true to a click that keeps failing actually hide?

level: seniorimportance: should knowfreq 47%

basics

~10 s

Forcing hides the failed check, not its cause. Playwright skips the actionability checks but still dispatches a real event at the computed point, so an overlay that owned that point still receives the click.

open as a page

In Playwright, what does locator.all() return, and why can a loop over its result be flaky?

level: seniorimportance: should knowfreq 52%

basics

~20 s

It returns one locator per currently matching element and waits for nothing. The array length is a snapshot, so a list still loading yields too few entries, and each entry is positional, so removing elements mid-loop shifts what the rest point at.

open as a page

A Playwright locator for issue cards matches three more elements than the board renders. How do you narrow it with locator.filter?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Read the set back first to see what the extras are, then exclude them by the property that really distinguishes them: visible true for unrendered nodes, hasNotText or hasNot for content, or a tighter starting locator for ancestors.

open as a page

A Playwright test stores page.frame('composer') and later fails with a detached-frame error after the issue view re-renders. Why, and what should it use instead?

level: seniorimportance: should knowfreq 34%

basics

~20 s

A Frame object is a reference to one live frame. When the issue view removes and re-creates the iframe, that frame is detached and every call through it fails. A frame locator re-resolves its selector on each use instead.

open as a page

Your Playwright test fails with 'Element is not attached to the DOM' after the issue board refreshes, so how do you find and fix the cause?

level: seniorimportance: should knowfreq 50%

basics

~20 s

That error comes from acting through an ElementHandle whose node was replaced. Find where the handle is created and stored, usually a page-object field or a hook, and replace it with a locator that re-resolves on every action.

open as a page

In a Playwright test, why does page.waitForLoadState() after a click often fail to make the test wait?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Because it inspects whichever document is current when the call runs. If the click has not committed a navigation yet, or never will because the app routes client-side, the old document already satisfies the state and it returns instantly.

open as a page

Why does page.getByRole('button', { name: 'Delete issue' }) find nothing in Playwright when that button is in the DOM?

level: seniorimportance: should knowfreq 57%

basics

~10 s

Most often the element is hidden for accessibility purposes. Role queries skip anything with aria-hidden, display none, visibility hidden or the hidden attribute, on itself or an ancestor, unless includeHidden is true.

open as a page

In Playwright, why can page.getByRole reach no element for a clickable div in an issue board's row menu?

level: seniorimportance: should knowfreq 44%

basics

~20 s

A div exposes no ARIA role, so no role query can select it. Playwright derives roles from an explicit role attribute or an implicit tag mapping, and div, span and an anchor without href have none.

open as a page

A Playwright locator using css= finds an issue tracker's submit button, but the equivalent xpath= matches nothing -- why?

level: seniorimportance: should knowfreq 36%

basics

~20 s

The button sits inside an open shadow root. Playwright's CSS engine pierces open shadow roots, so css= reaches it; the XPath engine does not cross a shadow boundary, so xpath= only ever finds the host element.

open as a page

A Playwright test that passed for months now fails with 'resolved to 2 elements' -- how do you diagnose it?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Read the candidate list in the error: it prints both matched nodes and a generated locator for each, which identifies the new duplicate. Then decide whether the page changed, or a hidden copy was always there and only now matches.

open as a page

When is Playwright's locator.first() or nth() a legitimate fix for a strict mode violation rather than a cover-up?

level: seniorimportance: should knowfreq 52%

basics

~20 s

Use first, last or nth when position genuinely is the requirement, such as the newest comment in a thread. Using them to silence an ambiguity you have not explained leaves a test that passes while acting on the wrong element.

open as a page

An issue card in a Playwright suite reads '3 comments' with a count that changes between runs — how do you locate it by text?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Pass a RegExp instead of a string: page.getByText with a pattern such as one matching digits followed by the word comments. The exact option is ignored for a RegExp, and the pattern is case-sensitive unless you add the i flag.

open as a page

A Playwright test's page.getByLabel('Assignee') finds nothing although the issue form clearly renders an Assignee label — why?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Because page.getByLabel searches controls, not label elements. It matches an element through aria-labelledby, aria-label, or a real HTML label association on a form control. A styled div next to a label element has none of those, so nothing matches.

open as a page

Should a Playwright suite auto-accept every dialog globally, or handle each one in its own test?

level: principalimportance: should knowfreq 35%

basics

~20 s

Handle them per test. A blanket accept-everything handler hides the dialogs a test should be asserting on, and turns a real regression -- a confirmation that stops appearing, or one that names the wrong issue -- into a silent pass.

open as a page

How do you decide when a Playwright suite may drive page.mouse and page.keyboard instead of locator verbs?

level: principalimportance: should knowfreq 33%

basics

~10 s

Treat the raw devices as a documented exception. page.mouse takes viewport coordinates and page.keyboard goes wherever focus already is, so neither re-resolves an element. Reserve them for gestures with no element to name.

open as a page

What does Playwright's locator.click({ trial: true }) actually do?

level: middleimportance: nice to knowfreq 28%

basics

~20 s

A trial run performs the actionability checks and then stops. Playwright waits for the element to be visible, stable, hit-testable and enabled, and never dispatches the click, so it verifies readiness without changing the page.

open as a page

In Playwright, when is passing an ElementHandle into page.evaluate() better than locator.evaluate()?

level: middleimportance: nice to knowfreq 28%

basics

~20 s

Use locator.evaluate() for one element: it resolves the locator, runs your function with that element and disposes the handle for you. Pass handles into page.evaluate() only when one function needs several live objects at once.

open as a page

What does the >> token do inside a Playwright selector string like css=article >> text=Reopen?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

The >> token chains selector parts: each part is queried relative to the previous part's result, like nested querySelector calls. Prefixing a part with an asterisk captures that intermediate element instead of the last one.

open as a page

A team wants every Playwright form interaction to use pressSequentially for realism. How do you evaluate that?

level: principalimportance: nice to knowfreq 28%

basics

~20 s

Replace the blanket rule with a criterion. Typing costs one round trip per character and adds a re-render window between each, so reserve it for fields whose own behaviour depends on key events and use fill everywhere else.

open as a page

Your team's Playwright suite is littered with explicit lifecycle waits; how do you decide which ones stay?

level: principalimportance: nice to knowfreq 30%

basics

~10 s

Rank the signals by how directly they express what the test needs. Assertions on rendered content first, page.waitForURL for routing, locator.waitFor for preconditions, page.waitForFunction as a documented last resort, and networkidle nowhere at all.

open as a page

When is registering a custom Playwright selector engine with selectors.register worth its maintenance cost?

level: principalimportance: nice to knowfreq 20%

basics

~20 s

Rarely. A custom engine earns its place only when a whole class of elements shares an addressing scheme the built-in engines cannot express and the application guarantees. Otherwise it adds a private query language every test author must learn.

open as a page

showing 31–59 of 59