74 / 139 · 13 Browser Automation with Playwright · Locator Strategies← prev⊞ allnext →☰ Read as one page
7.5Practical Example: Login Form
// BEST: role-based locators
await page.getByLabel('Email').fill('user@example.com');
await page.getByLabel('Password').fill('secret');
await page.getByRole('button', { name: 'Sign in' }).click();
// GOOD: test ID locators
await page.getByTestId('email-input').fill('user@example.com');
await page.getByTestId('password-input').fill('secret');
await page.getByTestId('login-button').click();
// AVOID: CSS selectors tied to implementation
await page.locator('input[type="email"]').fill('user@example.com');
await page.locator('input[type="password"]').fill('secret');
await page.locator('form.login-form button.btn-primary').click();