How to Handle Alerts and Popups in Selenium?

Learn via video courses
Topics Covered

Overview

Handling popups and alerts is critical to testing web applications using Selenium. In Selenium, popups and alerts can be handled using the Alert interface, which allows you to switch to the popup window, accept or dismiss the alert, and retrieve its text. For handling popups in Selenium, you need to identify the type of popup, switch to the popup window, interact with its elements, and switch back to the parent window. Handling popups and alerts effectively in Selenium ensures that your tests run smoothly and your web application functions correctly.

Introduction

Before heading to how to handle popup in selenium , let's get introduced to the webelements we will learn to handle.

Alerts, pop-ups, and multiple windows are common elements on web pages and can interrupt the user's flow. Selenium WebDriver provides built-in methods to handle these elements to avoid errors in test scripts and ensure a smooth user experience.

What are Alerts? Alerts are messages that pop up on the screen and require user interaction, such as clicking a button or entering data. There are three types of alerts: simple, prompt, and confirmation.

What are Pop-ups? Pop-ups are separate windows on top of the main window, often used for displaying advertisements, login prompts, or additional information. Selenium WebDriver can switch between multiple windows and handle pop-ups with built-in methods.

What are multiple Windows? Multiple windows are instances of web browsers that are open simultaneously. Selenium WebDriver provides methods to switch between multiple windows and access their elements, allowing testers to automate tasks that require interaction with multiple browser instances.

Types of Alerts in Selenium

Let's look into the different types of Alerts in Selenium.

Simple Alert

A simple alert is a popup window that contains a message and a single button, usually labeled "OK." These alerts convey information or notify the user of an event. In Selenium, we can handle simple alerts using the Alert interface. The Alert interface provides methods to accept or dismiss an alert and get the text contained in the alert.

Simple Alert

Prompt Alert

A prompt alert is similar to a simple alert but also includes a text box where the user can enter some input. These alerts are often used to get user input or to ask the user to confirm an action. In Selenium, we can handle prompt alerts using the Alert interface, just like simple alerts. We can also use the sendKeys() method to enter text into the prompt.

Prompt Alert

Confirmation Alert

A confirmation alert is similar to a prompt alert but only has two buttons: "OK" and "Cancel." These alerts are often used to confirm an action or to give the user a choice. In Selenium, we can handle confirmation alerts using the Alert interface, just like simple and prompt alerts. We can use the accept() method to click the "OK" button or the dismiss() method to click the "Cancel" button.

Confirmation Alert

How to handle Alert in Selenium WebDriver

In Selenium WebDriver, handling alerts is essential to web automation testing. Alerts are dialog boxes in a web application to notify users or request user input. Selenium provides methods to handle alerts and interact with them during test execution.

The following methods are available to handle alerts in Selenium WebDriver:

Void dismiss()

This method is used to dismiss or cancel an alert box. For example, if an alert box is displayed with the message "Are you sure you want to delete this item?" the dismiss() method can be used to cancel the action.

Void accept()

This method is used to accept or confirm an alert box. For example, if an alert box is displayed with the message "Are you sure you want to save changes?" the accept() method can be used to confirm the action.

String getText()

This method is used to get the text displayed in an alert box. For example, if an alert box displays "Please enter your name", the getText() method can retrieve the message.

Void sendKeys(String stringToSend)

This method is used to send text input to an alert box. For example, if an alert box is displayed with the message "Please enter your email", the sendKeys() method can be used to input the email address.

To handle alerts in Selenium, the Alert interface is used. The following code snippet demonstrates how to handle an alert in Selenium:

Code Example for Handling Simple Alert Here's an example of how to handle a simple alert in Selenium:

Code Example for Handling Prompt Alert Here's an example of how to handle a prompt alert in Selenium:

Code Example for Handling Confirmation Alert Here's an example of how to handle a confirmation alert in Selenium:

How to handle Pop-up windows using Selenium Webdriver

Before covering the steps on how to handle popup in selenium, we need to first uderstand what does popup stands for?

A pop-up window is a secondary browser window that appears on top of the main browser window. Pop-up windows can display advertisements, login forms, alerts, or other types of content that require the user's attention.

Pop-up windows

Pop-up windows can be triggered by user actions, such as clicking on a link or button, or they can be triggered by the website itself using JavaScript or other scripting languages. Now, let's look on how to handle popup in Selenium.

For handling popup in Selenium, you can use the getWindowHandles() and getWindowHandle() methods.

Driver.getWindowHandles();

The getWindowHandles() method returns a set of strings representing the window handles for all currently open windows. You can iterate through this set to switch between windows.

Driver.getWindowHandle();

The getWindowHandle() method returns a string representing the current window handle. You can use this to switch back to the parent window.

Here's an example code snippet that demonstrates how to handle popups in Selenium using these methods in Java:

Let's summarize the above steps on how to handle popup in Selenium:

  • The getWindowHandles() method returns a set of window handles, each representing a unique identifier for a window or tab.
  • The set of window handles returned by getWindowHandles() includes all the open windows or tabs, including pop-up windows and dialogs.
  • You can switch to a particular window or tab using the Driver.switchTo().window() method, passing the window handle as a parameter.
  • You can use a loop to iterate through the set of window handles and switch to each window in turn.
  • You can use the Driver.getWindowHandle() method to get the handle of the currently active window. This is useful when switching back to the main window after working with a pop-up window or dialog.

How to Handle Multiple Windows in Selenium

Handling multiple windows in Selenium is an essential task when automating web applications. Here are some steps to handle multiple windows in Selenium:

  1. Switch to a new window: To switch to a new window, you can use the getWindowHandles() method, which returns a set of window handles. You can then iterate through the set to switch to the desired window using the switchTo().window() method.
  2. Get the current window handle: To get the current window handle, you can use the getWindowHandle() method, which returns the handle of the current window.
  3. Close the current window: To close the current window, you can use the close() method.
  4. Close all windows except the current window: To close all windows except the current window, you can use the getWindowHandles() method to get a set of all window handles. You can then iterate through the set and close all windows except the current window using the close() method and the switchTo().window() method.
  5. Switch back to the default window: To switch back to the default window, you can use the switchTo().defaultContent() method.

Here's an example of how to handle multiple windows in Selenium:

Conclusion

  • Alerts, pop-ups, and multiple windows are common elements on web pages that can interrupt the user's flow.
  • Selenium WebDriver provides built-in methods to handle these elements and ensure a smooth user experience.
  • There are three types of alerts: simple, prompt, and confirmation.
  • Prompt and confirmation alerts have text boxes where users can enter input.
  • Selenium WebDriver allows switching between multiple windows and accessing their elements.
  • There are four methods to handle alerts in Selenium WebDriver: dismiss(), accept(), getText(), and sendKeys().
  • Pop-up windows can be triggered by user actions or websites using JavaScript or other scripting languages.
  • To handle pop-up windows in Selenium WebDriver, you can use the getWindowHandles() method to return a set of strings representing the window handles for all currently open windows.
  • You can use the getWindowHandle() method to return a string representing the current window handle, which can be switched back to the parent window.