Course → 1 · Selenium automation → Alerts, Frames & Windows
1 · Selenium automation
Handling JavaScript alerts
10 min · Module 12
Browser dialogs are not normal DOM elements. Use driver.switch_to.alert to accept, dismiss, or send text.
Example / notes
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("http://127.0.0.1:8000/lab/alert/")
driver.find_element(By.ID, "show-alert").click()
alert = driver.switch_to.alert
print(alert.text)
alert.accept()
driver.find_element(By.ID, "show-confirm").click()
driver.switch_to.alert.dismiss()
driver.find_element(By.ID, "show-prompt").click()
prompt = driver.switch_to.alert
prompt.send_keys("Pokhara")
prompt.accept()
print(driver.find_element(By.ID, "alert-result").text)
driver.quit()
Practice in Lab: /lab/alert/
Modules in this track
- 6. Getting Started
- 7. Finding Elements
- 8. Interactions
- 9. Waits
- 10. Assertions & Structure
- 11. Page Object Model
- 12. Alerts, Frames & Windows
- 13. Headless & Next Steps