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 pageshowhide
explore
- Query Builders20 questions
- Role and Name5 questions
- Text and Attribute Lookups5 questions
- Raw Selector Syntax6 questions
- Frame Traversal4 questions
- Narrowing a Match9 questions
- Strict Mode Violations4 questions
- Filtering and Chaining5 questions
- Interaction Gating15 questions
- Actionability Checks5 questions
- Handles and Staleness5 questions
- Load State Signals5 questions
- Acting on a Match15 questions
- Clicks, Keys and Drags5 questions
- Filling Form Controls5 questions
- Files and Dialogs5 questions
questions
page 2 of 2In Playwright's page.getByRole, when can you use the checked, pressed, expanded, selected and level options?
basics
~20 sOnly 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.
In a Playwright CSS selector, what is the difference between :has() and :has-text()?
basics
~20 sThe :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.
In Playwright, how do you restrict page.locator('button') to only the visible buttons?
basics
~20 sCall 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.
In a Playwright test, when an issue title sits inside a card, which elements does page.getByText('Fix login redirect') match?
basics
~20 sOnly 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.
In Playwright, how do you attach a file when the page renders no file input to target?
basics
~10 sWait 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.
In Playwright, what does adding force: true to a click that keeps failing actually hide?
basics
~10 sForcing 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.
In Playwright, what does locator.all() return, and why can a loop over its result be flaky?
basics
~20 sIt 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.
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?
basics
~20 sA 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.
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?
basics
~20 sThat 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.
In Playwright, why can page.getByRole reach no element for a clickable div in an issue board's row menu?
basics
~20 sA 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.
A Playwright locator using css= finds an issue tracker's submit button, but the equivalent xpath= matches nothing -- why?
basics
~20 sThe 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.
A Playwright test that passed for months now fails with 'resolved to 2 elements' -- how do you diagnose it?
basics
~20 sRead 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.
When is Playwright's locator.first() or nth() a legitimate fix for a strict mode violation rather than a cover-up?
basics
~20 sUse 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.
An issue card in a Playwright suite reads '3 comments' with a count that changes between runs — how do you locate it by text?
basics
~20 sPass 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.
A Playwright test's page.getByLabel('Assignee') finds nothing although the issue form clearly renders an Assignee label — why?
basics
~20 sBecause 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.
Should a Playwright suite auto-accept every dialog globally, or handle each one in its own test?
basics
~20 sHandle 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.
How do you decide when a Playwright suite may drive page.mouse and page.keyboard instead of locator verbs?
basics
~10 sTreat 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.
What does Playwright's locator.click({ trial: true }) actually do?
basics
~20 sA 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.
What does calling Playwright's page.frameLocator() with no selector do?
basics
~20 sIt starts the search in any frame of the subtree, main frame or iframe alike, so a test need not locate the iframe first. The rest of the chain still resolves in one frame; multi-frame matches error.
In Playwright, when is passing an ElementHandle into page.evaluate() better than locator.evaluate()?
basics
~20 sUse 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.
What does the >> token do inside a Playwright selector string like css=article >> text=Reopen?
basics
~20 sThe >> 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.
A team wants every Playwright form interaction to use pressSequentially for realism. How do you evaluate that?
basics
~20 sReplace 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.
When is registering a custom Playwright selector engine with selectors.register worth its maintenance cost?
basics
~20 sRarely. 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.
showing 31–59 of 59