Handling Popups

In Previous section we have discussed about the Right Click operation through the ActionChains class. Now in this section, we will discuss more about different types of popup such as alert and handling Popups and how to switch from one popup to another. This is most important topic which a selenium professional should learn. Addition to that we will also discuss about the handling Authentication Popup.

Objective:

  • Introduction to Popup
  • Web based Alert popup
  • Types of Alert
  • Handling Popups in Selenium Python

Introduction to Popup:

Let’s discuss each popup one by one for handling Popups. We will start with our basic popup which is known as Alert Popup.

Alert is a popup that comes up on the screen. These are different from the regular window and its blocking he main window for next operation. If you will access the main window’s content even if popup is open then you may get an exception called UnhandledAlertException which is a modal dialog.

There are two type of alert popup we should be focusing in automation testing.

  • Windows Based
  • Web Based(Browser Based)

In this section we will concentrate on the Web Based Alert Popup.

Web Based Alert Popup:

From the name you can guess the definition of the Web Based Alert Popup.

Yes. This is basically dealing with the web page. This popup may display on clicking any link on the page or on any action which need acknowledgement etc.

For example if a link requires authentication to view the page then it may ask you for the user name and password. Until you close or provide the user name and password you will not able to access that page or the main window.

So, we have to handle the popup in automation testing.

Types of Alerts:

We have 3 types of alert in web based popup. Below is the list of Alert type.

  • Simple Alert
  • Confirmation Alert
  • Prompt alert

Simple Alerts:

This alerts displays information and warning on the screen. There is some text in the popup along with the OK button. Until you click on the OK button or close the popup you will not able to access the main window.

Prompt Alert:

This alert needs some input from user. There is some text in popup along with text box and 2 button (Ok and Cancel). You need to pass some value to the text box and click the OK Button to close the popup or click on the cancel button.

Confirmation Alert:

This confirmation alert require permission to perform the action. You need to either accept or decline to the popup to handle with the popup.

How to handle Alert in Selenium Python:

First thing you need to remember that Alert is an Interface. If you need to learn more about Alert function then visit our Selenium Java Tutorial and come back and resume your learning with Selenium Python

There are mostly 4 methods need to handle Alert as below.

  • dismiss(
    • Alert alert = driver.switch_to.alert().dismiss();
  • accept();
    • Alert alert = driver.switch_to.alert().accept();
  • text();
    • Alert alert = driver.switch_to.alert().text();
  • send_keys();
    • Alert alert = driver.switch_to.alert().send_keys(“Text”);

To learn we will check http://demo.automationtesting.in/Alerts.html website.

Here you can see you have a button “click the button to display an alert box:”. Now if you click on the button then it will open an alert popup as below and you need to click on Ok to close the alert popup box.

In Alert popup  box you will not get any inspect element. In Selenium, you can inspect only Web Element and Alert Popup is not a web element. So you will not able to click on OK as there is no locator for OK.

Note: This alert is coming from “java Script”

To handle this alert we need a method named switch_to() which will help to switch our control from web page to alert popup.

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('http://demo.automationtesting.in/Alerts.html')
driver.maximize_window()

driver.find_element(By.CLASS_NAME, 'btn-danger').click()
time.sleep(5)
alert_box = driver.switch_to.alert
print(alert_box.text)
time.sleep(2)
alert_box.accept()

Explanation:

In the above example you can notice that we have used switch_to.alert to work with Alert popup. It will return the alert object. Once alert object is created you can use accept() with alert object to accept the popup. Alternatively this is same as clicking on OK in Alert popup.

Once you accept or close the Alert Popup, your driver got lost. To continue your work you need to come to the main webpage. To come to main page you need to use default_content.

driver.switch_to.default_content();

Note: While working with PyCharm, you can see there is another method called switch_to_alert is present. But this is deprecated and not in use. You need to use switch_to.alert to work with Alert popup.

Summary:

  • Alert is a popup that comes up on the screen. These are different from the regular window and its blocking he main window for next operation.
  • If you will access the main window’s content even if popup is open then you may get an exception called UnhandledAlertException which is a modal dialog.
  • There are two type of alert popup we should be focusing in automation testing. Window based and Web based.
  • This alert is coming from “java Script”

Now you are clear on handling Alert popups in Selenium Python. In next section, we will discuss more about the Frames and how to handle in Selenium Python.

Leave a Reply

Your email address will not be published. Required fields are marked *