- Selenium Features
- Selenium Installation
- Selenium Locators
- XPath Fundamentals
- CSS Selectors Methods
- Different Browsers Execution
- find_element & find_elements
- Check Enabled Status
- Check Displayed Status
- Check Selected Status
- Selenium Waits
- Send_keys Method
- Click Method
- Get Text
- Get Attribute Value
- Get Current URL
- Forward, Back, Refresh
- Take Screenshot
- Handle Browser Tabs
- Handle iframe
- Mouse Hover
- Context-Click
- Drag & Drop
- Handle Alerts
- Handle Dropdown
- Execute Javascript
- Scroll To element
- Headless Mode Execution
- Chrome Options
- Keyboard Action
Introduction to Handling Alerts in Python Selenium
Selenium is one of the most widely used frameworks for automating web browsers. It provides robust features to interact with web elements, navigate pages, and handle pop-ups efficiently. One of the critical aspects of web automation is handling alerts, which appear as pop-ups and require user interaction. In this comprehensive guide, we will explore the different types of alerts and how to handle them using Python Selenium.
Types of Alerts in Selenium
Alerts in Selenium can be broadly categorized into three types:
- Simple Alert – Displays a message with an OK button.
- Confirmation Alert – Displays a message with OK and Cancel buttons.
- Prompt Alert – Displays a message with an input field, allowing user input before clicking OK or Cancel.
Import Required Modules
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert
import time
Handling Simple Alerts in Selenium
A simple alert consists of a message and an OK button. Selenium provides the switch_to.alert
method to interact with alerts.
Example Code:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert
# Initialize WebDriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
# Open a webpage with an alert
driver.get("https://automationbysqatools.blogspot.com/2020/08/alerts.html")
# Click button to launch alert
driver.find_element(By.ID, "btnShowMsg").click()
time.sleep(2)
# Create alert object
alert = Alert(driver)
print(alert.text)
time.sleep(2)
# Accept alert
alert.accept()
# Close browser
driver.quit()
Handling Confirmation Alerts in Selenium
A confirmation alert contains an OK and Cancel button, requiring user interaction.
Example Code:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert
# Initialize WebDriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
# Open a webpage with an alert
driver.get("https://automationbysqatools.blogspot.com/2020/08/alerts.html")
confirm_btn = driver.find_element(By.ID, "button")
confirm_btn.click()
time.sleep(2)
# Create alert object
alert = Alert(driver)
print(alert.text)
time.sleep(2)
# Accept the alert
alert.accept()
UI_msg = driver.find_element(By.ID, "demo").text
print(UI_msg)
assert UI_msg == "You pressed OK!"
confirm_btn = driver.find_element(By.ID, "button")
confirm_btn.click()
# Dismiss the alert
alert.dismiss()
UI_msg_cancel = driver.find_element(By.ID, "demo").text
print(UI_msg_cancel)
assert UI_msg_cancel == "You pressed Cancel!"
# Close Browser
driver.quit()
Handling Prompt Alerts in Selenium
A prompt alert allows the user to enter input before dismissing the alert.
Example Code:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert
# Initialize WebDriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(10)
# Open a webpage with an alert
driver.get("https://automationbysqatools.blogspot.com/2020/08/alerts.html")
# Click button to launch prompt alert popup
prompt_box = driver.find_element(By.ID, "promptbtn")
prompt_box.click()
time.sleep(3)
# Create alert object
alert_prompt = Alert(driver)
new_value = "SQA"
# Send text to prompt box
alert_prompt.send_keys(new_value)
time.sleep(3)
# Accept the alert
alert_prompt.accept()
# Verify text on UI
ui_msg = driver.find_element(By.ID, "prompt")
expected_msg = f"Hello {new_value}! How are you today?"
assert ui_msg == expected_msg
time.sleep(5)
prompt_box = driver.find_element(By.ID, "promptbtn")
prompt_box.click()
time.sleep(3)
# Dismiss the prompt
alert_prompt.dismiss()
ui_msg = driver.find_element(By.ID, "prompt")
expected_msg = f"User cancelled the prompt."
assert ui_msg == expected_msg
# Close browser
driver.quit()
Best Practices for Handling Alerts in Selenium
- Use Explicit Waits: Instead of
time.sleep()
, use WebDriverWait to handle dynamic alerts. - Validate Alert Text: Always verify the alert text before taking action.
- Handle NoAlertPresentException: Wrap alert handling in a try-except block to avoid exceptions if no alert appears.
Example Using WebDriverWait
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.alert import Alert
# Initialize WebDriver
driver = webdriver.Chrome()
driver.get("https://automationbysqatools.blogspot.com/2020/08/alerts.html")
try:
WebDriverWait(driver, 10).until(EC.alert_is_present())
driver.find_element(By.ID, "btnShowMsg").click()
time.sleep(2)
alert = Alert()
print("Alert found: ", alert.text)
alert.accept()
except:
print("No alert present")
# Close Browser
driver.quit()