TestingDocs.com logoTestingDocs.com
Automation learning lab
TestingDocs • Self-paced course

Playwright Automation
Self Study Guide

Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.

60 chaptersBeginner friendlyHands-on practice
Playwright / Chapter 37 of 60~18 min • Beginner
Chapter 37Playwright

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

  1. Create a small form
  2. Find by id and class
  3. Assert the text

Example

TypeScript
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();
});
Expected result: Both assertions pass
💡 Point to remember
Avoid long brittle chains such as div:nth-child(5).

Practice application screenshot

Actual screenshot of the TestingDocs task manager practice application
TestingDocs practice page, captured in Chromium.

✎ 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