Selenium WebDriver-Handling Alerts
Last Updated :
24 Apr, 2025
Selenium is one of the most popular and powerful tools for automated web applications. Selenium is widely used for automating user interactions like filling out forms, navigating to a website clicking on a button, etc. While we are dealing with automating user interactions one of the most common scenarios while interacting with web applications is dealing with alerts.
What is an Alert?
Alerts are pop-up dialog boxes that appear on a web page to convey some important message to the user or prompt the user for some actions. There are three types of alerts you might encounter while interacting with web applications.
There are three types of Alerts:
1. Simple Alerts
It is the most basic type of alert. It is a simple alert that displays the message with an Okay button. It is commonly used to provide some important message to the user.
Example:
Selenium WebDriver-Handling Alerts2. Confirmation Alerts
It is a type of alert which is used to ask user for confirmation. It displays a message with a Okay and Cancel Button . It is commonly used for asking the user for intent to do something.
Example:
Selenium WebDriver-Handling Alerts3. Prompt Alerts
It is a type of alert which is used to collect input from the user. It displays a message with a input field, Okay and Cancel Button. It is commonly used to ask the user for some information or input.
Example:
Selenium WebDriver-Handling AlertsWhen to use Alerts in Selenium Web Driver?
While interacting with dynamic web application there can be times when we encounter an Alert in a website, alerts are the pop-up dialog boxes that appear on a website to convey some important messages, it requires the user or prompt the user for some action which we cannot ignore specially in writing testing scripts. In this case we'll have to handle the Alerts in Selenium Web Driver or it will effect the flow of our automation script. So in this article I'll show you a step-by-step tutorial on how to handle Alerts in Selenium.
How to Handle Alerts in Selenium?
Step 1: Installing the necessary Libraries.
Make sure you have python installed in your system then install Selenium using the following command.
pip install selenium
Step 2: Importing the required Modules.
We need to Import Web Driver and By from Selenium
- WebDriver: WebDriver provides a way to interact with the Browser programmatically. It acts a bridge between the automation script and your browser.
- By: By is a class in Selenium that provides a way to locate an element on a webpage using locators.
from selenium import webdriver
from selenium.webdriver.common.by import By
Step 3: Initializing the Web Driver and Navigating to the web page.
Python
from selenium import webdriver
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
url="http://127.0.0.1:5500/index.html"
driver.get(url)
Output:
Selenium WebDriver-Handling AlertsExplanation:
- webDriver.Chrome(): It sets up the Selenium WebDriver and selects Chrome as its automation Browser.
- driver.get(url): It is used to open Chrome Browser and navigate to the given URL.
Let us suppose as soon as we enter the URL the alert is triggered.
Step 4: Switching to the alert.
Let us suppose as soon as we entered the URL the alert gets triggered so now, we want our Selenium to focus on the alert. We can use switch_to.alert() method for this.
switch_to.alert: switch_to.alert is a method in selenium that allows us to switch the focus of the selenium web Driver to an alert pop up.
Python
from selenium import webdriver
driver=webdriver.Chrome()
url="http://127.0.0.1:5500/index.html"
driver.get(url)
alert=driver.switch_to.alert
Output-
Selenium WebDriver-Handling AlertsStep 5: Getting Text from Alert.
Getting text from the alert is often used to confirm whether we are interacting with the right alert box or not. In order to get text from the alert bar we'll use .text attribute to get text from the alert.
Python
from selenium import webdriver
driver=webdriver.Chrome()
url="http://127.0.0.1:5500/index.html"
driver.get(url)
alert=driver.switch_to.alert
print(alert.text)
Output:
Selenium WebDriver-Handling AlertsStep 6: Accepting the Alert.
Now that we have switch to the alert, we can now perform various actions like accepting or dismissing or getting text from the alert bar. Here is how we can accept the alert .
Python
from selenium import webdriver
driver=webdriver.Chrome()
url="http://127.0.0.1:5500/index.html"
driver.get(url)
alert=driver.switch_to.alert
print(alert.text) #.text to print the text inside the alert
alert.accept()
Output:
Selenium WebDriver-Handling AlertsExplanation:
- .text - .text return the text inside the alert box.
- .accept()- .accept() is used to click on the accept or okay button on the alert bar.
Step 7: Dismissing the Alert
Now that we have learn how to accept a alert we'll learn how to dismiss a alert. Selenium provides a .dismiss() method to dismiss or cancel an alert bar.
Python
from selenium import webdriver
driver=webdriver.Chrome()
url="http://127.0.0.1:5500/index.html"
driver.get(url)
alert=driver.switch_to.alert
print(alert.text) #.text to print the text inside the alert
alert.dismiss()
Output:
Selenium WebDriver-Handling AlertsExplanation:
.dismiss()- .dismiss() is used to click on the cancel button and dismiss the alert bar
Step 7: Sending keys to the Alert Bar (In case of Prompt Bar).
Prompt alerts are the alerts bars that asks the user some input along with Okay and Cancel Button. In that case .send_keys() are method in selenium which is used to stimulate keyboards input to the input bar or a text field.
Python
from selenium import webdriver
driver=webdriver.Chrome()
url="http://127.0.0.1:5500/index.html"
driver.get(url)
alert=driver.switch_to.alert
print(alert.text) #.text to print the text inside the alert
alert.send_keys("Your Input")
alert.accept()
Output:
Selenium WebDriver-Handling AlertsConclusion
Handling Alerts is one of the important steps in automating dynamic web applications, whether they are a simple alert, confirmation alert or prompt alert they all require a user interaction and can affect the flow of your automation testing. By using the above steps you'll be easily able to handle all types of alerts In Selenium and make your testing script more stable and reliable.
Similar Reads
Selenium WebDriver Event Listener
Testing Websites often includes testing multiple pages in the website. "Selenium" is one of the most popular test automated frameworks used to test multiple web pages provides numerous functionalities and enables the interaction between web pages. The name "Listeners" suggests that they can listen t
5 min read
Selenium WebDriver-Installation
Selenium WebDriver is a powerful tool for automating web applications for testing purposes. It allows developers and testers to write automated tests in various programming languages like Java, Python, C#, etc. Also, it supports different browsers like Firefox, Chrome, Edge, etc. for testing. Approa
2 min read
Features of Selenium WebDriver
Selenium is a powerful tool for controlling web browser through program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C# etc, we will be working with Python. This article revolves around Major Features of Selenium WebDriv
2 min read
Selenium WebDriver Commands
Selenium WebDriver is a powerful tool for automating the web browser to perform certain tasks. Selenium supports multiple browsers (such as Chrome, Firefox, Edge, Safari, etc.) and multiple programming languages (such as Java, Python, C#, etc.) so, it is very easy to use and automate tasks on a brow
7 min read
Selenium Webdriver Handling Checkbox using Java
A set of tools and libraries that allow the user to automate interactions with web browsers is known as Selenium. It is too diverse tool that can handle every operation of a webpage starting from opening the webpage to closing of webpage. In this article, we will see the various steps to handle the
6 min read
Limitations of Selenium Webdriver
Selenium is a powerful tool for controlling web browser through program. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc but it has some disadvantages and limitations such as it doesn't support Windows or Desktop app
2 min read
Selenium WebDriver - Browser Commands
Selenium WebDriver is an extremely useful tool for automating web applications and browser interactions. Through its collection of browser commands, developers and testers can control web browsers in a programmatic fashion. Browser commands that enable tasks such as web scraping, simplifying everyda
7 min read
Use of AutoIt in Selenium WebDriver
AutoIt is a scripting language used to automate the Windows GUI and general scripting tasks on the Windows platform. AutoIt can be used with Selenium WebDriver to handle scenarios where automation involves interactions with Windows-based GUI elements. This article focuses on discussing the use of Au
8 min read
Selenium WebDriver Handling Radio Buttons Using Java
A platform-independent language that is used to build various applications is known as Java. Java can also be used to automate the web drivers. There are various automation tools available, out of which Selenium is the most common one. We can automate the opening of the website, clicking of push but
4 min read
Selenium WebDriver - Navigation Commands
Selenium WebDriver is quite a popular open-source framework used for automating web browsers. It helps the developers to automate web-based operations and interactions by creating code in Java, Python, C#, and others. When automating tests, Selenium WebDriver offers a set of navigation commands that
6 min read