Skip to content

Locator Strategies

Playwright offers one of the most powerful locator engines in the testing ecosystem.

getByRole()

page.getByRole('button', { name: 'Login' });

getByTestId()

page.getByTestId('login-btn');

getByText()

page.getByText('Sign In');

CSS / XPath (last resort)

Why XPath Is Discouraged

  • Easily breaks with UI changes
  • Hard to maintain
  • Low readability
  • Causes flaky tests

Playwright Locators Are

  • More stable
  • Easier to read
  • Auto-wait enabled