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 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()
url="http://127.0.0.1:5500/index.html"
driver.get(url)
alert=driver.switch_to.alert


Output-

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()
url="http://127.0.0.1:5500/index.html"
driver.get(url)
alert=driver.switch_to.alert
print(alert.text)


Output:

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()
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 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()
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 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()
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 Alerts

Selenium WebDriver-Handling Alerts

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.

Similar Reads

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

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

Conclusion

...