Blog

How to Master Alert and Popup Handling in Selenium: A Step-by-Step Guide

Selenium automation companies

For website owners, ensuring a seamless user experience is an absolute necessity. Imagine this: your website visitors encounter unexpected alerts and pop-ups while navigating your platform. These interruptions can hinder your customer experience. If users can efficiently deal with these pop-up windows, they might stay, and you’d earn more potential users.

In website ownership, your ability to handle these occasional disruptions with finesse is the key to turning these obstacles into stepping stones to a flawlessly functioning website.

We’ll demonstrate in this blog how to automate these pop-up and alert tests in Selenium. It is a popular method for testing windows and pop-up notifications on your website or application.

What Are Alerts In Selenium?

Alerts are little pop-up boxes that show messages or notifications. They inform the user and might ask for permission for specific actions. They can also be employed to provide warnings.

Users can sometimes type in some information in the alert box. Application alerts force you to open a new browser window rather than your current one. They force you to read the message and follow its instructions.

Different Types Of Alerts/Popups You Might Encounter

1. Simple Alert

This little window has an “OK” button and a message. It helps show information or notify the user about something. In Selenium, we can deal with simple alerts using the Alert interface, which offers methods to either accept or dismiss the alert and retrieve the message in the alert.

2. Prompt Alert

Like the first alert, it offers a textbox where users can input text. Prompt alerts are helpful when you need user input or confirmation. With this, you can use the Alert interface to manage prompt alerts and the sendKeys() function to input text into the prompt.

3. Confirmation Alert

It is similar to a prompt alert having only two buttons: “OK” and “Cancel.” When you need the user to affirm an action or make a decision, you use them. Like simple and prompt alerts, the Alert interface manages confirmation alerts in Selenium. You can use the accept() method to click “OK” or the dismiss() method to click “Cancel.”

4. Authentication Alerts

These are messages that ask for your username and password. They’re often helpful on websites where you need to log in. Examples of real-world authentication alerts are:

  • A box that pops up, asking for your username and password to access a protected API.
  • A message that requests your username and password when trying to connect to a VPN.
  • A request for your login credentials, including your username and password, to access a banking website.

5. Warning/Error Alerts

These alerts show messages about issues or errors. They frequently include a message and an “OK” button. You see these alerts when you’re using digital devices. Here are some examples:

  • Your device needs more storage space.
  • You’ve attempted to log in to a website too frequently.
  • You’ve entered the wrong UPI PIN for a payment.
  • You’re trying to delete a critical system file.

How To Handle Alerts In Selenium WebDriver?

When Selenium automation companies work with Selenium WebDriver to automate tasks, it usually focuses on the main web browser window. However, if a pop-up or alert appears, it opens a new window. We need to shift our focus from the main window to the alert window to handle these alerts. Selenium WebDriver offers a command for this:

driver.switchTo().alert()

Once we’ve switched our focus to the alert window, we can use methods from the Alert Interface to perform various actions, like accepting the alert, dismissing it, retrieving its text, or entering text into it.

For example:

  • To click the ‘OK’ button in the alert, use:driver.switchTo().alert().accept()
  • To handle the ‘Cancel’ button, use: driver.switchTo().alert().dismiss()
  • To get the text from the alert box, use: driver.switchTo().alert().getText()
  • And to send some text to the alert box, use: driver.switchTo().alert().sendKeys(“Text”)

These commands help us manage alerts effectively while using Selenium WebDriver.

What Are Popups In Selenium?

When something happens, a popup, which resembles a small window, opens on your screen. If you want to learn about different types of popups and how to deal with them, check out the documentation page. When you’re automating tasks on a website with multiple windows, you have to switch between them to get things done. After you’re done, you need to go back to the main window, which is like the starting window in Selenium.

Handling Web Dialog Box/Popup Window Using Selenium

In Automation Testing Company, when we work with web applications with multiple windows, we sometimes need to switch between these windows to perform tasks. After finishing the task, we return to the main window. Let’s explore this with an example.

Selenium web driver, we have methods to handle multiple windows:

Driver.getWindowHandles(): This method helps us manage all open windows and allows us to switch between them in a web application. A list of window handles is provided.

Driver.getWindowHandle(): When the website initially opens, we use this method to handle the main window. It uniquely identifies the current window within the driver instance. It returns a string.

To handle windows of Selenium, follow these steps:

Now, we’ll automate a scenario to demonstrate how to handle multiple windows using Selenium Webdriver.

Step 1: Open the website.

Launch a web browser and go to the website.

Step 2: select the “Click Here” link.

The “Click Here” link will open a new child window when you click on it.

Step 3: A New Child Window Opens.

You must fill out the form in the new window by entering your email address.

Step 4: Submit your email ID information.

Step 5: After submitting the page, display the access credentials.

After executing the code, you will see that the child window has opened in a new tab.

  • Close the child window where the credentials are displayed.
  • Switch back to the parent window.

How To Handle Multiple Windows In Selenium?

Selenium window management is essential for automating web applications. Here are a few short steps to accomplish that:

  • Switch to a new window

The getWindowHandles() method can be used to switch to a different window. It provides you with a variety of window handles. The switchTo().window() method can browse this collection and switch to the desired window.

  • Get the current window handle

The getWindowHandle() method can determine which window is currently open. QA Testing Company provides the identifier for the window you’re now in.

  • Close the current window

The close() method can be used to close the current window.

  • Close all windows other than this one

You can use the getWindowHandles() function to collect all window handles and close all but the one you’re currently in. After that, you can go through them and employ switchTo() and the close() method.window() can be used to close all but the window you’re interested in.

  • Reset the window to its default setting

By using the switchTo() function, you can go back to the original/default window.method defaultContent().

Final Thoughts

Web pages often have things like notifications, small boxes, or extra windows that can disrupt what you’re doing. Selenium WebDriver has tools to deal with these and make your experience smoother. There are three kinds of notifications: simple ones, ones where you can type stuff and ones where you confirm something. Selenium WebDriver can switch between those extra windows and say “no” or “yes,” getting text or typing.

The extra windows can pop up when you click on stuff or websites that use unique code. To deal with these in Selenium WebDriver, you can use methods like getWindowHandles() to see all the open windows and getWindowHandle() to return to the main one.

The following two tabs change content below.
AutomationQA

AutomationQA

Co-Founder & Director, Business Management
AutomationQA is a leading automation research company. We believe in sharing knowledge and increasing awareness, and to contribute to this cause, we try to include all the latest changes, news, and fresh content from the automation world into our blogs.