arrow-left Back to topicQuizLocatorsCheck yourself1.A developer renames the CSS class on a Submit button from .btn-submit to .btn-primary. Which locator will NOT break?page.locator('.btn-submit')page.getByRole('button', { name: 'Submit' })page.locator('button.btn-primary')2.You have a table with 20 order rows. You want to click the 'Cancel' button only in the row that contains 'ORDER-042'. What do you use?page.getByText('Cancel').nth(5).click() — count the row manuallypage.getByRole('row').filter({ hasText: 'ORDER-042' }).getByRole('button', { name: 'Cancel' }).click()page.locator('tr:has-text("ORDER-042") button.cancel').click()3.Playwright throws 'strict mode violation: locator resolved to 5 elements' when you call .click(). What does this mean?The page has a JavaScript error that prevents clickingThe locator matches 5 elements — Playwright refuses to click an ambiguous targetThe click timed out after 5 retries4.An element has no accessible role, no label, and no stable visible text. When is getByTestId the right choice?Always — getByTestId is the most reliable locator for every element type.Never — test IDs couple tests to implementation details.When the element genuinely has no semantic role or accessible name — for example a custom chart, canvas, or drag-and-drop card — and developers add a data-testid attribute as an explicit test hook.Only in test.beforeEach setup steps, not in assertions.5.What does page.getByRole('listitem').nth(2) return?The last list item on the page.A locator scoped to the third list item (index 2, zero-based).The second list item (index 2, one-based).It throws because nth() is only valid on locator() calls, not getByRole().6.When is getByAltText the appropriate locator?When you want to find any element with a visible text label.When finding an <img> element (or similar) by its alt attribute — useful for image-based buttons or icons that have no visible text.For tooltip text that appears on hover.Only for <area> elements inside image maps.7.You want to find the 'Edit' button only inside a specific row of a table. How should you write this?page.getByRole('button', { name: 'Edit' }) — Playwright automatically picks the right one.page.locator('tr button:text("Edit")') — CSS with text pseudo-class narrows it down.page.getByRole('row').filter({ hasText: 'ORDER-007' }).getByRole('button', { name: 'Edit' })page.getByRole('button', { name: 'Edit', within: 'tr' })8.If you call .click() on a locator that matches 3 elements, what happens?Playwright clicks all 3 elements in sequence.Playwright clicks the first matching element automatically.Playwright throws a strict mode violation error because the locator is ambiguous.The click is silently skipped and the test continues.Submit answersarrow-left PreviousRunning and debugging testsActionsNext arrow-right