---
title: Locator Strategies Knowledge Check | TestingBot
description: '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…'
source_url:
  html: https://testingbot.com/resources/courses/locator-strategies/knowledge-check
  md: https://testingbot.com/resources/courses/locator-strategies/knowledge-check.md
---

Course

[Selenium Locators and Locator Strategies](https://testingbot.com/resources/courses/locator-strategies)

Chapter 12 of 12

1. [1 Selenium Locators: Strategies That Survive Change](https://testingbot.com/resources/courses/locator-strategies)
2. [2 Why Tests Break on Locators](https://testingbot.com/resources/courses/locator-strategies/why-locators-break)
3. [3 Inspect the DOM and Test Selectors in DevTools](https://testingbot.com/resources/courses/locator-strategies/inspect-and-test-selectors-in-devtools)
4. [4 CSS Selectors for Test Automation](https://testingbot.com/resources/courses/locator-strategies/css-selectors)
5. [5 XPath in Selenium: Text, Axes and When to Use It](https://testingbot.com/resources/courses/locator-strategies/xpath-in-selenium)
6. [6 Role-Based, Label and Relative Locators](https://testingbot.com/resources/courses/locator-strategies/role-based-and-relative-locators)
7. [7 A Test ID Contract With Your Developers](https://testingbot.com/resources/courses/locator-strategies/a-test-id-contract)
8. [8 Shadow DOM and iframes](https://testingbot.com/resources/courses/locator-strategies/shadow-dom-and-iframes)
9. [9 Lists, Tables and Dynamic Content](https://testingbot.com/resources/courses/locator-strategies/lists-tables-and-dynamic-content)
10. [10 A Note on Mobile Locators](https://testingbot.com/resources/courses/locator-strategies/a-note-on-mobile-locators)
11. [11 A Locator Review Checklist](https://testingbot.com/resources/courses/locator-strategies/locator-review-checklist)
12. [12 Locator Strategies Knowledge Check](https://testingbot.com/resources/courses/locator-strategies/knowledge-check)

[Exit course](https://testingbot.com/resources/courses)

 Chapter 12 of 12 Intermediate 5 min read Updated Nov 2026 

# 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](https://testingbot.com/resources/courses/locator-strategies/certification) uses a different set of questions.

1. Which property of an element is the safest thing for a locator to depend on?

 Its position among its siblings Its CSS classes A test id attribute that nothing else in the codebase uses The id the framework generated for it

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 page defines its own `$`, such as jQuery, which hides the DevTools helper `$` only works with XPath The element is inside a shadow root `$` requires the Elements panel to be open

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?

 `label > input[required]` `input[required] label` `label[required]` `label:has(input[required])`

`: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?

 `//button[text()='Save changes']` `//button[normalize-space()='Save changes']` `//button[@text='Save changes']` `//span[text()='Save changes']`

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 last cell of every paid row Every paid row that is the last row of its table The last row in the document that has a cell with the text "Paid" Nothing, because `last()` is XPath 2.0

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?

 Several links match, so the locator must be scoped or narrowed before acting The link is not visible yet and needs a wait Role locators do not work on links The accessible name is case-sensitive

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?

 Rename every attribute in the app to `data-testid` Set `testIdAttribute: 'data-qa'` under `use` in the Playwright config Use `getByTestId('data-qa=checkout')` It is not possible; use CSS instead

`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?

 `driver.switch_to.frame(host)`, then `find_element` An XPath with `//`, which searches all trees A longer explicit wait on the document-level locator Find the host element, take its `shadow_root`, then `find_element` with a CSS selector

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?

 Catch the exception and click again on the same element Add `time.sleep(2)` before the click Keep the locator and find the element again after the re-render Use an absolute XPath, which cannot go stale

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 id, with the same value on both platforms CSS selectors XPath copied from the inspector Screen coordinates

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.

Check answers

[Previous · Chapter 11 A Locator Review Checklist](https://testingbot.com/resources/courses/locator-strategies/locator-review-checklist)
## 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.

[Start free trial](https://testingbot.com/users/sign_up) [See pricing](https://testingbot.com/pricing) [Browse other courses](https://testingbot.com/resources/courses)

## TestingBot Locator Strategies Certification

Turn this course into a free, resume-worthy credential: pass the exam, run the course suite on TestingBot, and get a verifiable certificate for your LinkedIn profile.

[How it works](https://testingbot.com/resources/courses/locator-strategies/certification)
