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 webdriver
import time
#specify where your chrome driver present in your pc
PATH=r"C:\Users\educative\Documents\chromedriver\chromedriver.exe"
#get instance of web driver
driver = webdriver.Chrome(PATH)
#provide website url here
driver.get("https://omayo.blogspot.com/")
#sleep for 2 seconds
time.sleep(2)
#get button and click on it to get alert
driver.find_element("id","alert1").click()
#switch to alert box
alert = driver.switch_to.alert
#sleep for a second
time.sleep(1)
#accept the alert
alert.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 webdriver
import time
#specify where your chrome driver present in your pc
PATH=r"C:\Users\educative\Documents\chromedriver\chromedriver.exe"
#get instance of web driver
driver = webdriver.Chrome(PATH)
#provide website url here
driver.get("https://omayo.blogspot.com/")
#sleep for 2 seconds
time.sleep(2)
#click on the link to get popup
popup_link = driver.find_element("xpath", '//*[@id="HTML37"]/div[1]/p/a').click()
#get instance of first pop up window
whandle = driver.window_handles[1]
#switch to pop up window
driver.switch_to.window(whandle)
#get text of a element in pop window
print(driver.find_element("id","para1").text)
#sleep for 1 second
time.sleep(1)
#closes current window
driver.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_handles method 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.

Copyright ©2024 Educative, Inc. All rights reserved