---
title: 'Selenium Locators and Locator Strategies: Free Online Course'
description: 'A free course on Selenium locators and locator strategies: CSS selectors,
  XPath, role-based and test-id locators in Selenium and Playwright, shadow DOM, iframes,
  tables, and a review checklist for locators that last.'
source_url:
  html: https://testingbot.com/resources/courses/locator-strategies
  md: https://testingbot.com/resources/courses/locator-strategies.md
---

Course

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

Chapter 1 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 1 of 12 Intermediate 4 min read Updated Nov 2026 

# Selenium Locators: Strategies That Survive Change

A free course on Selenium locators and locator strategies: CSS selectors, XPath, role-based and test-id locators in Selenium and Playwright, shadow DOM, iframes, tables, and a review checklist for locators that last.

## What you will learn

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

[Continue where you left off](https://testingbot.com#)

## 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)On this page
1. [Locator strategies at a glance](https://testingbot.com#locator-strategies-at-a-glance)
2. [How the course is organised](https://testingbot.com#how-the-course-is-organised)
3. [The practice pages](https://testingbot.com#the-practice-pages)
4. [What you'll build](https://testingbot.com#what-you-ll-build)
5. [Prerequisites](https://testingbot.com#prerequisites)

_Last reviewed November 2026 against Selenium 4.52 (Python), Playwright 1.65 and Chrome 157._

A locator is the part of a test that says _which_ element to act on. Selenium calls them `By` strategies (`By.ID`, `By.CSS_SELECTOR`, `By.XPATH` and the rest); Playwright calls the objects themselves locators (`page.getByRole(...)`, `page.locator(...)`). Whatever the framework, the locator is where most UI tests break: not because the feature broke, but because a class name, a wrapper `div` or the order of a list changed.

This course is about choosing locators that keep working. It is not a tour of the `By` API (the [Selenium Python course](https://testingbot.com/resources/courses/selenium-python/locators) and the [Playwright tutorial](https://testingbot.com/resources/courses/playwright/locators) each cover their framework's API in one chapter). Instead it answers the questions that come after: which strategy to reach for first, how to write CSS and XPath that do not encode layout, when XPath is genuinely the right tool, how role-based locators and test ids work, and what to do with shadow DOM, iframes, tables and lists that change while you look at them.

## Locator strategies at a glance[#](https://testingbot.com#locator-strategies-at-a-glance)

| Strategy | Selenium | Playwright | Survives a redesign? |
| --- | --- | --- | --- |
| Test id | `By.CSS_SELECTOR, "[data-testid='save']"` | `getByTestId('save')` | Yes, nobody else uses it |
| Role and accessible name | `accessible_name` to check, no direct `By` | `getByRole('button', { name: 'Save' })` | Yes, unless the visible text changes |
| Label | XPath on the `<label>` | `getByLabel('Email')` | Yes, unless the label changes |
| Stable `id` or `name` | `By.ID`, `By.NAME` | `locator('#email')` | Yes, if the id is not generated |
| CSS on meaningful attributes | `By.CSS_SELECTOR, "button[type='submit']"` | `locator("button[type='submit']")` | Usually |
| XPath on text and structure | `By.XPATH, "//tr[td[normalize-space()='Doe']]"` | `locator('xpath=...')` | When written relative, yes |
| Absolute path, layout classes, positions | `/html/body/div[2]/...`, `.col-md-4 > div:nth-child(3)` | same | No |

The rest of the course explains each row, shows where it fails and runs every example.

## How the course is organised[#](https://testingbot.com#how-the-course-is-organised)

1. Why tests break on locators, and what it costs
2. Inspecting the DOM and testing selectors in DevTools
3. CSS selectors in depth
4. XPath in depth, and when it is the right tool
5. Role-based, label and relative locators
6. A test id contract with your developers
7. Shadow DOM and iframes
8. Lists, tables and dynamic content
9. A short note on mobile locators
10. A locator review checklist
11. A knowledge check

## The practice pages[#](https://testingbot.com#the-practice-pages)

Every example runs against public practice pages that have been stable for years, so you can paste the code and watch it pass:

- `the-internet.herokuapp.com`: a login form, sortable data tables (one with helpful classes, one without), a "challenging DOM" with generated ids, nested frames and controls that appear and disappear.
- The Selenium project's own test pages at `www.selenium.dev/selenium/web/`: a web form with labels, a page with a web component that has a shadow root, and a page with an iframe.
- `demo.playwright.dev/todomvc`: a todo app whose markup carries `data-testid` attributes.

The Selenium examples are Python with pytest; the Playwright examples are TypeScript with Playwright Test. Both ran locally and on the TestingBot grid in Chrome and Firefox on Windows 11. If you write Selenium in Java, every `By` strategy and every CSS and XPath expression in this course is identical; only the surrounding syntax changes.

## What you'll build[#](https://testingbot.com#what-you-ll-build)

A small Selenium suite and a small Playwright suite that:

- find the same elements with CSS, XPath, labels, roles and test ids, and show which ones break,
- reach into a shadow root and into nested frames,
- pick a row out of a table by its content and read a column after sorting,
- run on two browsers on TestingBot with every session marked passed or failed,
- come with a short script that flags brittle locators in code review.

## Prerequisites[#](https://testingbot.com#prerequisites)

- Python 3.10 or newer with `pip install selenium pytest`, and/or Node.js 20 or newer with `npm init playwright@latest`.
- Chrome on your machine. Selenium Manager downloads the matching driver for you.
- Basic HTML: you know what an element, an attribute and a class are.
- A TestingBot account for the cloud runs and the optional [certification](https://testingbot.com/resources/courses/locator-strategies/certification) (the free trial is enough).

If you want to try a selector before writing any code, paste the page's HTML into our free [XPath and CSS selector tester](https://testingbot.com/free-online-tools/xpath-css-selector-tester); it shows every match and generates the Selenium, Cypress and Playwright call for you.

[Next · Chapter 2 Why Tests Break on Locators](https://testingbot.com/resources/courses/locator-strategies/why-locators-break)

On this page

1. [Locator strategies at a glance](https://testingbot.com#locator-strategies-at-a-glance)
2. [How the course is organised](https://testingbot.com#how-the-course-is-organised)
3. [The practice pages](https://testingbot.com#the-practice-pages)
4. [What you'll build](https://testingbot.com#what-you-ll-build)
5. [Prerequisites](https://testingbot.com#prerequisites)

Certification

Earn the free, resume-worthy TestingBot Locator Strategies Certification for this course.

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

## Other free courses

- [Mobile App Testing: From First Test to CI A free, hands-on mobile app testing course: emulators vs real devices, Appium vs Espresso vs XCUITest vs Ma...](https://testingbot.com/resources/courses/mobile-app-testing)
- [Accessibility Testing Tutorial: WCAG 2.2 A free, hands-on accessibility testing course: WCAG 2.2 in practice, manual keyboard and screen reader chec...](https://testingbot.com/resources/courses/accessibility-testing)
- [Manual Testing Course A hands-on manual testing course: test design, test cases, exploratory charters, bug reports developers act...](https://testingbot.com/resources/courses/manual-testing)
- [Flaky Tests: Find, Fix and Prevent Fix flaky tests for good: measure them, then remove the four root causes (waits, shared data, test order, r...](https://testingbot.com/resources/courses/flaky-tests)

[Browse all courses](https://testingbot.com/resources/courses)
