selenium-webdriver Handle an alert Python

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

There are multiple ways to switch to alert pop-up in Python:

  1. Deprecated:
alert = driver.switch_to_alert()
  1. Using switch_to:
alert = driver.switch_to.alert
  1. Using ExplicitWait:
 from selenium.webdriver.common.by import By
 from selenium.webdriver.support.ui import WebDriverWait
 from selenium.webdriver.support import expected_conditions as EC

 alert = WebDriverWait(driver, TIMEOUT_IN_SECONDS).until(EC.alert_is_present())
  1. By declaring the instance of Alert class:
from selenium.webdriver.common.alert import Alert

alert = Alert(driver)

To fill input field in pop-up triggered by JavaScript prompt():

alert.send_keys('Some text to send')

To confirm dialog pop-up*:

alert.accept()

To dismiss:

alert.dismiss()

To get text from pop-up:

alert.text

*P.S. alert.dismiss() could be used to confirm pop-ups triggered by JavaScript alert() as well as alert.confirm()



Got any selenium-webdriver Question?