Lesson 2: Functions and Selectors

Finding Elements: Selectors

Selectors are the strings used to point to elements on a web page.

Best Practices

1. User-facing Locators: getByRole, getByLabel, getByPlaceholder.

2. Resilience: Avoid long CSS paths like `div > ul > li:nth-child(2)`.

Example

javascript

// Recommended

await page.getByRole('button', { name: 'Log in' }).click();

// Fallback

await page.locator('#submit-btn').click();

    CareerRaah - AI Learning Platform for Professionals