DEV Community

Mangai Ram
Mangai Ram

Posted on • Originally published at testleaf.com

How Playwright Finds Buttons and Text More Smartly Than Selenium

This post talks about another testing tool called Playwright.

Do you want to learn playwright contact testleaf

Think of Playwright as a modern version of Selenium — it does the same job (checking whether a website works) but uses smarter ways to find things on the screen.

Instead of depending on long, fragile paths like Selenium used to, Playwright looks for meaningful labels such as:

the role of an element (like button, textbox, link)

or a special tag (called a testId) that developers can add.

These new ways make tests more stable, readable, and future-proof.

Simple Example

Let’s imagine a “Login” button again.

With Selenium, a tester might have written:

// old fragile way
page.$("//div[3]/button").click();

Enter fullscreen mode Exit fullscreen mode

If the page design changes, this line fails.

With Playwright, you can write:

// smart new way
await page.getByRole('button', { name: 'Login' }).click();

Enter fullscreen mode Exit fullscreen mode

Here Playwright automatically finds the visible button that says “Login.”
Even if the developer changes its position or layout, your test still works.

Why It’s Better

Fewer failures when the website changes.

Easy to read — the code looks almost like plain English.

No extra waiting — Playwright automatically waits until the button is clickable.

Works with modern websites built in React, Angular, or Vue.

What is getByRole() and getByTestId()?

getByRole() looks for elements based on their function — for example:

page.getByRole('textbox', { name: 'Email' });

Enter fullscreen mode Exit fullscreen mode

means “Find the text box called Email.”

getByTestId() uses a tag added by developers:

<input data-testid="email-input" />

Enter fullscreen mode Exit fullscreen mode

and in Playwright:

page.getByTestId('email-input').fill('user@testleaf.com');

Enter fullscreen mode Exit fullscreen mode

It’s like giving every element a name tag that never changes.

Mixing Both for Best Results

Many testers use both together:

const loginBtn = page.getByRole('button', { name: /login/i });
await expect(loginBtn).toBeEnabled();
await loginBtn.click();

Enter fullscreen mode Exit fullscreen mode

This makes tests extra strong and human-friendly.

Auto-Wait Feature

A big win with Playwright is that it waits automatically for things to load or become clickable — no need to tell it to “wait 5 seconds.”
That means fewer timing errors and faster scripts.

Migration from Selenium to Playwright:

| Selenium Way                           | Playwright Way                                |
| -------------------------------------- | --------------------------------------------- |
| `By.xpath("//button[text()='Login']")` | `page.getByRole('button', { name: 'Login' })` |
| `By.id("submit")`                      | `page.getByTestId('submit')`                  |

Enter fullscreen mode Exit fullscreen mode

In Short

Playwright is faster and more intelligent.

It finds buttons and fields using roles or tags, not messy paths.

Tests are cleaner, shorter, and more reliable.

Great for any company using modern web apps.

Top comments (0)