Handling Alert, Confirm, and Prompt Dialogs in Playwright

Handling Alert, Confirm, and Prompt Dialogs in Playwright

In Selenium, automated testing often encounters various dialogs: alert, confirm, and prompt. These are common scenarios in business applications. Therefore, when learning the Playwright framework, these situations are also inevitable. Today, I will share how to handle these three types of dialogs in the Playwright framework.

First, let’s understand the differences between these three types of dialogs:

alert: Only has an OK button. For example, some error messages.

confirm: Confirmation dialog, which can be canceled or accepted. For example, when deleting something, the dialog can have both cancel and confirm options.

prompt: A dialog that requires input, such as a common login form.

Handling Alert, Confirm, and Prompt Dialogs in Playwright
Handling Alert, Confirm, and Prompt Dialogs in Playwright
In the code above, we first define a handle_dialogs function to handle the three different types of dialogs. For the alert dialog, we simply accept it; for the confirm dialog, we can choose to accept or reject as needed; for the prompt dialog, we can pass in a custom input to accept the dialog. Finally, in the main function, we start the browser and call the handle_dialogs function to trigger the dialogs.
This is just a sample code. For specific practical operations, you can follow Teacher Minjie in our Python automation course. If you encounter any issues, feel free to contact Teacher Minjie anytime~~~
Handling Alert, Confirm, and Prompt Dialogs in Playwright

Leave a Comment