Playwright · Junior · Question 56 of 75
How do you accept a confirm() dialog?
page.on("dialog", (dialog) => dialog.accept());
await page.getByRole("button", { name: "Delete" }).click();- 1Register a dialog handler before the click
- 2Press Enter after the dialog appears
- 3Click the dialog's OK button with a locator
Show the answer
Correct answer: Register a dialog handler before the click
Explanation
Native dialogs aren't part of the DOM, so locators can't reach them. The handler must be registered before the action that opens the dialog.