How do I perform a click action on a button with specific text using Playwright?


To click on a button with text "foo" in @playwright/test, follow these steps:

  1. Locate the button element using the getByText() method.
  2. Call the click() method to perform a simple human click.

Here's an example code snippet:

await page.getByText('foo').click();

If you need to perform a double-click or right-click instead of a simple click, use the dblclick() method for double-click and pass { button: 'right' } as an option to click() for right-click.

Sometimes, apps may use non-trivial logic where hovering over an element overlays it with another element that intercepts the click. If this causes issues with your test, bypass actionability checks and force the click by passing { force: true } as an option to click(). However, use this only when necessary.

With these methods, you can easily click on a button with text "foo" using @playwright/test.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.