There could be a couple of reasons why Playwright can't find the button in a modal.
Firstly, your page might be suffering from poor hydration. This is when the page sends a static version to the browser before the dynamic part that makes it "live". If Playwright interacts with the page before its listeners have been added, the click won't have any effect.
You can check if this is the issue by using Chrome DevTools and selecting "Slow 3G" network emulation in the Network panel. If button clicks are ignored and entered text is reset during page load, you've got poor hydration.
The fix? Make sure all interactive controls are disabled until after hydration.
Secondly, clicking an element in Playwright could trigger multiple navigations. In this case, you should explicitly wait for a specific URL using page.waitForURL()
after clicking an element.
Here's a TypeScript example:
await page.click('#my-button');
await page.waitForURL('https://ray.run/expected-url');
Lastly, dialogs in web pages are modals and they block further execution until they are handled. If there's no listener for page.on('dialog')
, all dialogs are automatically dismissed by default. So, make sure your action handles or dismisses any dialogs encountered during execution.
page.on('dialog', async dialog => {
console.log(dialog.message());
await dialog.dismiss();
});
Remember, these are just a few possible scenarios. For more tips on handling flaky tests in Playwright, check out this blog post.
If you still have questions, please ask a question and I will try to answer it.
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 [email protected].