#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Playwright Interview Q&A · #9

What are selectors in Playwright?

By Uncodemy Editorial Team · Updated 16 Nov 2026

A selector is the description Playwright uses to find an element on the page: "the button called Enrol", "the input labelled Email", "the element with data-testid="cart-count"". In modern Playwright you rarely write raw selector strings. You build a locator with methods such as page.getByRole(), and the locator finds the element fresh every time you use it, waiting for it to appear.

The built-in locators, in order of preference

The Playwright docs recommend locators that match what a user sees, because they survive markup changes better than class names do.

LocatorFindsExample
getByRoleElements by ARIA role and accessible namegetByRole('button', { name: 'Enrol' })
getByLabelForm fields by their labelgetByLabel('Email')
getByPlaceholderInputs by placeholder textgetByPlaceholder('you@example.com')
getByTextNon-interactive textgetByText('Order placed')
getByAltTextImages by alt textgetByAltText('Uncodemy logo')
getByTitleElements by title attributegetByTitle('Close')
getByTestIdElements by data-testidgetByTestId('cart-count')

When none of these fit, page.locator() accepts CSS ('li.card') or XPath ('xpath=//li[@class="card"]'; a string starting with // is also treated as XPath).

Example: a course listing page

// tests/enrol.spec.ts
import { test, expect } from '@playwright/test';

test('enquire about the Data Science course', async ({ page }) => {
  await page.goto('/courses');

  // narrow down: the list item that mentions Data Science, then its button
  const card = page.getByRole('listitem').filter({ hasText: 'Data Science' });
  await card.getByRole('button', { name: 'Details' }).click();

  await page.getByLabel('Email').fill('student@example.com');
  await page.getByRole('button', { name: 'Send enquiry' }).click();

  await expect(page.getByText('We will contact you')).toBeVisible();
});

Chaining (card.getByRole(...)) searches inside the first locator. filter({ hasText }) and filter({ has: otherLocator }) pick the right item from a list without relying on its position. Use .first(), .last() or .nth(i) only when position really is the thing you are testing.

Strict mode

Actions such as click() and fill() expect the locator to match exactly one element. If two buttons are both named "Enrol", the click fails at once with an error that starts:

locator.click: Error: strict mode violation: getByRole('button', { name: 'Enrol' }) resolved to 2 elements

The error lists both elements and suggests a more precise locator. This is deliberate: clicking "whichever one comes first" hides real bugs. Narrow the locator by chaining or filtering instead.

Shadow DOM and iframes

Tips

Want to master Playwright hands-on?

Join Uncodemy's Playwright Automation Testing Course in Noida — Dedicated Placement Support.

Explore the Course →