Chapter 37: DOM and CSS selectors
Automation learning lab
Playwright Automation
Self Study Guide
Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.
DOM and CSS selectors
The DOM represents HTML as a tree of nodes. CSS selectors can match element types, ids, classes and attributes. A locator describes how to find an element when an action occurs. Prefer semantic locators when available; CSS is useful for stable explicit contracts.
How it works
Step-by-step walkthrough
- Create a small form
- Find by id and class
- Assert the text
Example
import { test, expect } from '@playwright/test';
test('chapter 37 practice', async ({ page }) => {
await page.setContent('<div id="status" class="notice">Ready</div>');
await expect(page.locator('#status')).toHaveText('Ready');
await expect(page.locator('.notice')).toBeVisible();
});Avoid long brittle chains such as div:nth-child(5).
Practice application screenshot
✎ Your practice task
Add a data-state attribute and locate it with CSS.
Knowledge check
Which CSS selector matches an id of status?
🌐 Continue learning
60-chapter learning path
Start at chapter 1, or choose any topic. Your progress is tracked separately for each chapter.
JavaScript fundamentals
TypeScript fundamentals
Playwright • setup & locators
Playwright • interactions & assertions
Playwright • framework & debugging
Playwright • real-world automation
Interactive practice lab
Try the controls below and observe the corresponding Playwright locator instructions. This browser demo does not execute Node.js or Playwright Test; copy the code to your local Playwright project to automate it.
Task manager demo
Suggested automation
Actions on the demo update these examples. In your local test use a page that contains the same controls.
Download a standalone lab page, then serve it locally (for example with Python's http.server). Navigate to it in a Playwright test.