What is Locator?
A locator in Playwright is a way to find elements on the page at any moment. It does not grab an element once. Every time you act on it, Playwright finds the up-to-date element, which makes locators the core of its auto-waiting and retries. Locators are also strict: an action throws if several elements match.
No account needed · Scored in under a minute against a senior rubric
What does Locator mean in practice?
Playwright recommends built-in locators that follow what a user sees. page.getByRole() finds elements by accessibility role and name. page.getByText() finds text content, page.getByLabel() finds a form control by its label, and page.getByPlaceholder(), page.getByAltText() and page.getByTitle() cover placeholders, image alt text and title attributes. page.getByTestId() reads the data-testid attribute by default. The docs advise putting user-facing attributes and explicit contracts, such as getByRole, first.
A locator is resolved again before every action. If you call locator.hover() and then locator.click(), Playwright finds the element twice. When the DOM re-renders between the two calls, the click uses the new element instead of failing on a stale one.
Strictness catches vague locators. page.getByRole('button').click() throws when the page has more than one button. You can opt out with locator.first(), locator.last() or locator.nth(), but the docs do not recommend it: when the page changes, Playwright may click an element you did not intend. The better fix is a locator that matches one element, such as page.getByRole('button', { name: 'Sign in' }).
Why do interviewers ask about Locator?
Locator choice decides how often a suite breaks when the UI changes. Interviewers use it to see whether you test against what users see or against brittle markup paths.
What does Locator look like in a real project?
A sign-in test used a long CSS selector copied from the browser's developer tools. A layout change wrapped the button in a new element and the test failed, though sign-in still worked. The team switched to page.getByRole('button', { name: 'Sign in' }). The next redesign changed the markup again and the test kept passing, because the role and name stayed the same.
How should you talk about Locator in an interview?
Name the order you reach for locators: role first, then label or text, then test ids. Explain strictness, and why reaching for first() to silence a strict mode error hides a locator that is not specific enough.
Go Deeper on Locator
Know the term. These are the courses that turn the concept into something you can demonstrate.
Common questions about Locator
What happens when a Playwright locator matches more than one element?
Actions that need one target element throw a strict mode violation error. Fix the locator so it matches exactly one element, for example by adding the accessible name to getByRole. locator.first(), locator.last() and locator.nth() opt out of the check, but Playwright's docs do not recommend them.
Should I use getByTestId or getByRole in Playwright?
Playwright's docs recommend user-facing locators such as getByRole first. Test ids are the most resilient option, because the test still passes when the text or role changes, but they are not user facing. If the role or text matters to what you are testing, use a user-facing locator.
Which terms relate to Locator?
Explore related glossary terms to deepen your understanding.
Related Resources
Dive deeper with these related interview prep pages.
Free QA career tools, no account needed
Instant and private, everything runs in your browser. Try them before you sign up.
QA Resume Checker
Instant 0-100 score on automation keywords, impact, and ATS formatting.
QA Cover Letter Generator
A tailored 3-paragraph QA cover letter from your resume and a job post.
QA Application Tracker
Drag-and-drop kanban to track every QA application from Applied to Offer.
QA Take-Home Test Generator
A realistic take-home assignment with a scenario, tasks, and a rubric.
QA LinkedIn Headline Generator
A recruiter-searchable headline, About section, and skills list.
QA STAR Story Builder
Structure a QA behavioral answer with the STAR method and instant checks.
QA Bug Report Generator
Build a clean, reproducible bug report for Markdown, Jira, or plain text.
Boundary Value Analysis Generator
Generate boundary value and equivalence partitioning test cases from a range.
QA Metrics Calculator
Calculate DRE, defect leakage, defect density, and pass rate with interpretation.
QA Test Plan Generator
Build a structured test plan (scope, approach, criteria, risks) in Markdown.
QA Salary Calculator
Estimate QA, SDET, and automation tester pay by level, market, and skills.
QA Offer Evaluator
See total comp, a counter range, and a ready-to-send negotiation message.
Ready to Ace Your QA Interview?
Practice explaining locator and other key concepts with our AI interviewer.
Join 600+ QA engineers already practicing with AssertHired.
A test passes locally but fails in CI about one run in five. Walk me through what you check first, and why.
This single answer is scored on four dimensions: Technical accuracy · Coverage · Clarity · Best practices.
Rather skip ahead? Create a free account