How to handle an alert and pop-up window in Selenium using Python
Overview
Selenium is an open-source web-based automation tool. We'll learn how to handle alert and pop-up windows in Selenium using Python. Let's take a look at each one separately.
Handle alert window
We'll handle alerts using the following methods.
-
driver.switch_to.alert: This method will switch focus to the alert box and return an instance of the alert. alert.accept(): This method will accept the alert.alert.dismiss(): This method will dismiss the alert box.
Example
Let's take a look at an example code of this.
from selenium import webdriverimport time#specify where your chrome driver present in your pcPATH=r"C:\Users\educative\Documents\chromedriver\chromedriver.exe"#get instance of web driverdriver = webdriver.Chrome(PATH)#provide website url heredriver.get("https://omayo.blogspot.com/")#sleep for 2 secondstime.sleep(2)#get button and click on it to get alertdriver.find_element("id","alert1").click()#switch to alert boxalert = driver.switch_to.alert#sleep for a secondtime.sleep(1)#accept the alertalert.accept()
Explanation
- Line 11: We use the
driver.get()method to open the given URL. - Line 17: We find the button element which generates an alert box after clicking on it.
- Line 20: We switch the focus to the alert box and get an instance of it.
- Line 26: We use the
accept()method to accept the alert box.
Handle pop-up window
We'll handle the pop-up window using the following methods.
driver.window_handles[1]: This method gets the instance of the pop-up window.driver.switch_to.window(handle): This method switches to the given pop-up window.driver.close(): This method closes the current window.
Example
from selenium import webdriverimport time#specify where your chrome driver present in your pcPATH=r"C:\Users\educative\Documents\chromedriver\chromedriver.exe"#get instance of web driverdriver = webdriver.Chrome(PATH)#provide website url heredriver.get("https://omayo.blogspot.com/")#sleep for 2 secondstime.sleep(2)#click on the link to get popuppopup_link = driver.find_element("xpath", '//*[@id="HTML37"]/div[1]/p/a').click()#get instance of first pop up windowwhandle = driver.window_handles[1]#switch to pop up windowdriver.switch_to.window(whandle)#get text of a element in pop windowprint(driver.find_element("id","para1").text)#sleep for 1 secondtime.sleep(1)#closes current windowdriver.close()
Explanation
- Line 17: We get the link that generates a popup window after clicking on it.
- Line 20: We get the instance of the popup window. We use the
driver.window_handlesmethod to get the child windows. - Line 23: We switch to the popup window by passing the instance of the popup window as a parameter to
switch_to.window()method. After switching focus to the popup window, we can interact with it like a regular window. - Line 32: We use the
driver.close()method to close the current window, a popup window in our case.
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved