Browser Automation with Python and Selenium — 12: Managing Alerts
How to handle alert dialogs

In the previous post, we looked at working with cookies. We will explore the handling of alert dialogs in this post.
An alert is a pop-up window. It gets triggered due to some action performed by the user or automatically due to some system settings. Their purpose is to give some information to the user, take permission, or take some input from the user.
Selenium WebDriver Alert API provides methods to handle interactions with these pop-up message boxes.
We can categorize the alerts into the following three types:
- Simple Alert
- Confirmation Alert
- Prompt Alert
Simple Alert
A simple alert shows a custom message and a single button(usually an “OK”) which dismisses the alert.
When the alert pop-up appears on the web page, control still remains with the parent. We need to switch control to the alert to be able to take action on it.
You can accept an alert with the accept() method, cancel it with the dismiss() method, fill prompts with the send_keys() method, or get the text of the alert with the text property.
In the following example, an alert is created by executing JavaScript code on the page. Then we switch to the alert, read the text, and close it by calling the accept method on it.







