arrow-left Back to topicQuizOther locatorsCheck yourself1.Your page has a table of orders. Each row has an order number and a 'Cancel' button. You want to click Cancel for the row with order number 'ORD-1042'. There's no unique test id on the button. What's the cleanest approach?Use XPath to navigate: //tr[contains(., 'ORD-1042')]//buttonUse locator.filter(): page.getByRole('row').filter({ has: page.getByText('ORD-1042') }).getByRole('button', { name: 'Cancel' }).click()Use nth= to pick the right row by its index in the tableUse page.locator('tr:has-text("ORD-1042")').getByRole('button', { name: 'Cancel' }).click()2.What does the :visible CSS pseudo-class do in Playwright selectors?It selects elements with visibility: visible CSS property onlyIt filters the locator result to only include elements that are currently visible on the page, excluding hidden duplicates:visible is standard CSS and is not specific to PlaywrightIt selects elements that are within the visible viewport area3.What is the recommended priority order for choosing a locator strategy in Playwright?CSS selector > XPath > getByText > getByRolegetByRole > getByLabel > getByText > getByTestId > CSS/XPath as last resortgetByTestId > getByRole > CSS selector > XPathAll strategies are equal — use whichever is most concise4.What does nth=-1 select when used with a locator?It throws an error — negative indices are not supportedThe last element in the locator result setThe second-to-last element in the locator result setOne element before the first match — useful for ancestors5.You need to locate an input field that has no label or placeholder, but is positioned to the right of the text 'Username'. Which locator should you use?page.locator('input').nth(0) — assume it's the first input on the pagepage.locator('input:right-of(:text("Username"))') — layout-based locatorpage.getByText('Username').locator('..') — go to the parent elementpage.locator('xpath=//input[preceding-sibling::*[text()="Username"]]')6.What is the key difference between :has-text() and :text() CSS pseudo-classes in Playwright?:has-text() is case-sensitive; :text() is case-insensitive:has-text() matches any ancestor element containing the text anywhere inside; :text() matches the smallest element that directly contains the text:text() supports regex patterns; :has-text() only supports plain stringsThey are identical — both find the smallest element containing the text7.Why are XPath locators considered a last resort in Playwright?XPath is slower to execute than CSS selectors in the browserXPath locators are tied to DOM structure — any HTML restructuring breaks them, making tests fragile and hard to maintainXPath is not supported in Firefox and WebKit in PlaywrightPlaywright's auto-waiting doesn't work with XPath locators8.A page has both a visible dropdown and a hidden mobile dropdown with the same CSS class. page.locator('.dropdown').click() throws a strict mode violation. What is the cleanest fix?Use page.locator('.dropdown').first().click() to always pick the first matchUse page.locator('.dropdown:visible').click() to target only the visible elementAdd a unique id attribute to the visible dropdown and target thatUse page.locator('.dropdown').nth(0).click() — first() and nth(0) are the sameSubmit answersarrow-left PreviousMock browser APIsSnapshot testingNext arrow-right