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
- Selenium Locators: Strategies That Survive Change
- Why Tests Break on Locators
- Inspect the DOM and Test Selectors in DevTools
- CSS Selectors for Test Automation
- XPath in Selenium: Text, Axes and When to Use It
- Role-Based, Label and Relative Locators
- A Test ID Contract With Your Developers
- Shadow DOM and iframes
- Lists, Tables and Dynamic Content
- A Note on Mobile Locators
- A Locator Review Checklist
- Locator Strategies Knowledge Check
On this page
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 and the Playwright tutorial 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#
| 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#
- Why tests break on locators, and what it costs
- Inspecting the DOM and testing selectors in DevTools
- CSS selectors in depth
- XPath in depth, and when it is the right tool
- Role-based, label and relative locators
- A test id contract with your developers
- Shadow DOM and iframes
- Lists, tables and dynamic content
- A short note on mobile locators
- A locator review checklist
- A knowledge check
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 carriesdata-testidattributes.
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#
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#
- Python 3.10 or newer with
pip install selenium pytest, and/or Node.js 20 or newer withnpm 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 (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; it shows every match and generates the Selenium, Cypress and Playwright call for you.