top of page

Window Handling And Alerts in Selenium

A window in any browser is the main web page on which the user lands after hitting a URL. Such a window in Selenium is referred to as the Parent Window or Main Window. It opens when the Selenium WebDriver session is created and has all the focus of the WebDriver.

For example, when you open any website or link, the page you land in is the Main Window.

How to identify parent and child windows in Selenium

When you open any website, the main page where you will perform any operation is the Parent Window. This is the same webpage that will open when the Selenium automation script is executed.ll the other windows that open inside your main window are termed as Child Windows.It is important to note that there can be single or multiple child windows inside your parent window.It is important to note that there can be single or multiple child windows inside your parent window.

Now we are going to see different methods used in window handling

  • getWindowHandle(): With this method, we get a unique ID of the current window which will identify it within this driver instance. This method will return the value of the String type.

  • getWindowHandles( ): With this method, we get the IDs of all the windows opened by the web driver. Its return type is Set .

  • switchTo(): Using this method, we can perform switch operations within windows.

How to handle single child window

We can Iterate through child windows,then get the handles of all the windows that are currently open using the command: Set<String> allWindowHandles = driver.getWindowHandles(); which returns the set of handles.We use the SwitchTo command to switch to the desired window and also pass the URL of the web page.

How to handle multiple child windows

When it comes to handling multiple child windows in Selenium, the overall process remains the same as we do for a single child window. However, in the case of multiple child windows, you can use any of the available conditional operators like if-else, switch etc. and do the operations, which are particular to each child window.

How to switch back from child window to parent window

Now, once the WebDriver has control of the child window, it has control only on that and you can no longer perform any operation in the parent window. So how do we switch back to the parent window in case we need to?

This can be simply done by using the switchTo() function. This function helps in shifting the focus back to the parent window.

Let's see what are Alerts

  • Alerts are small popup windows that display the message/notifications and notify the user with some information or may ask for permission on certain kinds of operation.

  • There are two types of alerts: Web/Javascript/browser-based alerts and Windows/OS-based alerts in Selenium. Web-based alerts can further bifurcate into Simple alerts, Prompt alerts, and confirmation alerts.

  • These alerts are significantly visible in online application forms, banking websites, and social networking/Email service provider websites like Gmail. Every QA must have encountered such alerts while automating the application under test.

Here are the most commonly used methods to handle alerts in Selenium:

  1. driver.switchTo().alert() - This method switches the focus of the WebDriver to the alert window.

  2. alert.accept() - This method clicks the OK button on the alert window.

  3. alert.dismiss() - This method clicks the Cancel button on the alert window.

  4. alert.sendKeys("text") - This method sends text to the alert window. (Note: Not all alerts accept input)

Here's an example of how to handle an alert using Selenium WebDriver

// Switch to the alert window

Alert alert = driver.switchTo().alert();

// Get the alert text
String alertText = alert.getText();

// Click the OK button on the alert window

// Send text to the alert window
alert.sendKeys("Hello, world!");

// Click the Cancel button on the alert window

We can either accept or dismiss the alert message box.

How to handle unexpected Alerts using Selenium Webdriver

Sometimes when we are browsing any web application, unexpected alerts occur due to some error or some other reasons. This kind of alert does not display every time you open the site, but they surface only at random intervals. If you have created an automation test case for such pages and not handled this kind of alerts, then your script will fail immediately if such unexpected alert pop up is displayed.

We have to handle these unexpected alerts specifically, and for that, we can use the try-catch block.

if we write direct code(without try-catch) to accept or dismiss the alert, and If the alert does not appear, then our test case will fail to throw any exception in Selenium like timeout Exception. Try catch block can handle both situations.

The following code demonstrates how we handle unexpected alerts in Selenium using a try-catch block.

import org.openqa.selenium.*;

public class alerts {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();

        try {
            WebDriverWait wait = new WebDriverWait(driver,10);
            Alert simpleAlert = driver.switchTo().alert();
            System.out.println("Unexpected alert accepted");
        } catch (Exception e) {
            System.out.println("unexpected alert not present");


It's important to note that not all alerts are created equal. Some alerts may have different properties and methods that can be used to interact with them, depending on the web application being tested. It's always a good idea to consult the application's documentation or inspect the alert element in the browser's developer console to determine the appropriate methods to use when handling alerts in Selenium.

53 views0 comments

Recent Posts

See All


Avaliado com 0 de 5 estrelas.
Ainda sem avaliações

Adicione uma avaliação
bottom of page