avatarCoşkun Deniz

Summary

This context provides a detailed guide on how to handle alert dialogs using Selenium WebDriver Alert API in Python.

Abstract

The context discusses the three types of alerts: Simple Alert, Confirmation Alert, and Prompt Alert. It explains how to accept, cancel, fill prompts, and get the text of the alert using the accept(), dismiss(), send_keys(), and text property methods, respectively. The context also highlights the need to switch control to the alert with the switch_to method to take action on it. It further explains the exceptions raised when trying to send input to a simple or confirm alert using the send_keys method and when trying to switch to alerts before they are displayed. The context concludes by providing references for further reading.

Bullet points

  • Alerts are pop-up windows triggered due to user actions or system settings.
  • There are three types of alerts: Simple, Confirmation, and Prompt.
  • Alerts can be handled using Selenium WebDriver Alert API in Python.
  • The accept() method is used to accept an alert, dismiss() to cancel it, send_keys() to fill prompts, and text property to get the text of the alert.
  • Control needs to be switched to the alert with the switch_to method to take action on it.
  • ElementNotInteractableException is raised if input is tried to be sent to a simple or confirm alert using the send_keys method.
  • NoAlertPresentException is raised when trying to switch to alerts before they are displayed.
  • Explicit wait can be used to avoid NoAlertPresentException.
  • References are provided for further reading.

Browser Automation with Python and Selenium — 12: Managing Alerts

How to handle alert dialogs

Photo by Anna Tarazevich from Pexels

In the previous post, we looked at working with cookies. We will explore the handling of alert dialogs in this post.

An alert is a pop-up window. It gets triggered due to some action performed by the user or automatically due to some system settings. Their purpose is to give some information to the user, take permission, or take some input from the user.

Selenium WebDriver Alert API provides methods to handle interactions with these pop-up message boxes.

We can categorize the alerts into the following three types:

  1. Simple Alert
  2. Confirmation Alert
  3. Prompt Alert

Simple Alert

A simple alert shows a custom message and a single button(usually an “OK”) which dismisses the alert.

When the alert pop-up appears on the web page, control still remains with the parent. We need to switch control to the alert to be able to take action on it.

You can accept an alert with the accept() method, cancel it with the dismiss() method, fill prompts with the send_keys() method, or get the text of the alert with the text property.

In the following example, an alert is created by executing JavaScript code on the page. Then we switch to the alert, read the text, and close it by calling the accept method on it.

# output
Alert text: Custom Alert

Confirmation Alert

A confirmation alert is similar to a simple alert. There is an extra Cancel button to dismiss the alert. The same API methods can also be used for this type of alert.

Prompt Alert

A prompt alert has one more feature than a confirmation alert which is a text input field to take some input from the user.

This input field can be filled with the send_keys() method.

The following example creates a prompt dialog and types “Python” into it.

Execution of the handling_promt_alerts.py

ElementNotInteractableException is raised if you try to send input to simple or confirm alert using the send_keys method.

InvalidElementStateException : class
        ^
        |
    ElementNotInteractableException : class
    ElementNotSelectableException : class
    ElementNotVisibleException : class

ElementNotInteractableException is derived from InvalidElementStateException.

# output
Received ElementNotInteractableException with message: User prompt of type alert is not interactable

When you try to switch to alerts before they are displayed, you get NoAlertPresentException. To avoid this situation, we can wait for alerts to appear with an explicit wait.

The following example tries to immediately switch to the alert which is created after 3 seconds timeout and NoAlertPresentException is received. At the second attempt, an explicit wait is used with the alert_is_present condition and properly switches to alert and closes it by the accept method.

# output
Received NoAlertPresentException
Switching to alert with explicit wait
Closed alert dialog

Things to Remember

  • An alert is a pop-up window that is triggered due to some action performed by the user or automatically due to some system settings.
  • There are three types of alerts: Simple, Confirmation, and Prompt.
  • You can accept an alert with the accept() method, cancel it with the dismiss() method, fill prompts with the send_keys() method, or get the text of the alert with the text property.
  • You need to switch control to the alert with the switch_to method to be able to take action on it.
  • ElementNotInteractableException is raised if you try to send input to a simple or confirm alert using the send_keys method.
  • NoAlertPresentException is raised when you try to switch to alerts before they are displayed.

In the next post, we will look at interacting with select elements.

Thank you for your time.

References

  1. https://www.selenium.dev/documentation/en/webdriver/js_alerts_prompts_and_confirmations/
  2. https://www.techbeamers.com/handle-alert-popup-selenium-python/
  3. http://allselenium.info/python-selenium-handle-alerts-prompts-confirmation-popups/
Python
Selenium
Automation
Programming
Technology
Recommended from ReadMedium