Open In App

Selenium WebDriver-Handling Alerts

Last Updated : 26 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Simple Alert

Selenium WebDriver-Handling Alerts

2. 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:

Confirmation Alert

Selenium WebDriver-Handling Alerts

3. 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:

Prompt Alert

Selenium WebDriver-Handling Alerts

When 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()
driver.get(url)


Output:

alert4

Selenium WebDriver-Handling Alerts

Explanation:

  • 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()
driver.get(url)
alert=driver.switch_to.alert


Output-

alert5

Selenium WebDriver-Handling Alerts

Step 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()
driver.get(url)
alert=driver.switch_to.alert
print(alert.text)


Output:

alert6

Selenium WebDriver-Handling Alerts

Step 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()
driver.get(url)
alert=driver.switch_to.alert
print(alert.text) #.text to print the text inside the alert
alert.accept()


Output:

alert7

Selenium WebDriver-Handling Alerts

Explanation:

  • .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()
driver.get(url)
alert=driver.switch_to.alert
print(alert.text) #.text to print the text inside the alert
alert.dismiss()


Output:

alert8

Selenium WebDriver-Handling Alerts

Explanation:

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

alert9

Selenium WebDriver-Handling Alerts

Conclusion

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads