DEV Community

Cover image for Handling Visibility in Playwright: getByText vs. getByRole
3

Handling Visibility in Playwright: getByText vs. getByRole

When automating web testing with Playwright, handling visibility properly is crucial, especially when elements share the same text. In this post, we'll explore how Playwright's locator methods handle visibility and how to ensure your tests interact with the right elements.

The Scenario: Multiple Buttons with the Same Text

Imagine you have a web page with two buttons: one is hidden, and one is visible. Both buttons contain the text "Submit":

<button style="display: none">Submit</button>
<button>Submit</button>
Enter fullscreen mode Exit fullscreen mode

If you attempt to interact with the "Submit" button using getByText, you might run into an issue.

How getByText and getByRole Differ

When you use getByText to locate the button, Playwright may find multiple matching elements, including hidden ones:

await page.getByText('Submit').click();
Enter fullscreen mode Exit fullscreen mode

Since getByText does not automatically filter out hidden elements, you might unintentionally target an invisible button.

On the other hand, getByRole behaves differently—it only finds visible elements by default:

await page.getByRole('button', { name: 'Submit' }).click();
Enter fullscreen mode Exit fullscreen mode

This difference in behavior means that getByRole will not run into the same issue as getByText in cases where hidden elements are present.

Explicitly Filtering for Visibility

To make getByText and other locators behave like getByRole, you need to explicitly filter for visibility. Adding .filter({ visible: true }) ensures only visible elements are considered, avoiding unintended interactions:

await page.getByText('Submit').filter({ visible: true }).click();
Enter fullscreen mode Exit fullscreen mode

This approach works not only for getByText but also for other locator methods like locator() that do not automatically filter out hidden elements.

Conclusion

Understanding how different Playwright locators handle visibility can help ensure that your tests interact with the right elements. getByRole inherently filters out hidden elements, whereas getByText and other locators do not, which can lead to unintended interactions. If you're using getByText or other locators, remember to explicitly filter for visibility when needed.

Happy testing with Playwright!

Useful links

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay