Locator Strategies Knowledge Check
Ten questions across the course. Pick an answer for each and check them at the end; every answer comes with an explanation. This is a self-check: the answers are in the page source. The certification exam uses a different set of questions.
1. Which property of an element is the safest thing for a locator to depend on?
Positions change with data and layout, classes with styling, and generated ids with every render. A test-only attribute has no other reason to change.
2. In the DevTools Console, $('#cart') returns something that is not an element, but $$('#cart') returns one element. What is going on?
The console helpers are only defaults: a page that defines $ wins. A shadow root would hide the element from $$ as well.
3. Which CSS selector selects the <label> elements that contain a required input?
:has() selects the element that contains a match. Option a selects the inputs, b looks for labels inside inputs, and c looks for a required attribute on the label itself.
4. A button is <button><span>Save</span> changes</button>. Which XPath matches it by its full visible text?
The button's own text node is only " changes"; "Save" belongs to the span. normalize-space() uses the string value of the whole element, descendants included, with whitespace collapsed.
5. What does (//tr[td='Paid'])[last()] return?
The parentheses apply [last()] to the whole result set. Without them, it would apply per parent and return paid rows that happen to be last in their tbody. last() is part of XPath 1.0.
6. In Playwright, page.getByRole('link', { name: 'Edit' }).click() fails with a strict mode violation. What does that tell you?
Strict mode refuses to act on a locator with more than one match. Scope it to the row or card it belongs to, for example with filter({ hasText }).
7. Your team uses data-qa instead of data-testid. How do you make getByTestId('checkout') work?
testIdAttribute tells getByTestId which attribute to read, for the whole project or per file with test.use().
8. In Selenium, how do you find a button inside an open shadow root?
The shadow root is a separate tree that document-level searches do not enter. It is not a frame, and XPath is not supported inside it.
9. A Selenium test stores a WebElement for a list item, triggers a filter that re-renders the list, then clicks the stored element. It fails with StaleElementReferenceException. What is the right fix?
The stored reference points to a node that no longer exists. No wait brings it back and every locator type produces references that can go stale. Re-finding by locator gets the new node.
10. Which locator strategy should a native Android and iOS test reach for first?
Accessibility ids are resolved natively, are fast and work across platforms. CSS does not exist in native apps, copied XPath is slow and brittle, and coordinates change with every screen size.
You finished Selenium Locators and Locator Strategies
Put what you learned into practice. Run your tests on 6100+ real browsers and physical iOS and Android devices.