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();
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.

Further reading

Problem with a question

Playwright · Junior · question 56

Test yourself

Take the Playwright · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz