Handling Windows and Frames Using Selenium With Java

Introduction

In this article, we will understand how to handle windows and frames using Selenium with Java.

What is Handling Windows?

Web applications often open new windows or pop-ups, presenting a challenge for us. Selenium provides methods to switch between different browser windows.

The browser window often called the main or parent window, represents the homepage or the currently open web page a user sees when opening a browser. When a Selenium automation script runs, it typically starts with the parent window.

Selenium WebDriver session involves opening a window that is initially controlled by the WebDriver.

When we click on a button or URL link in the parent window, and the action opens another window(s) within the main window, the new window(s) is called a child window.

Fetching Window Handles

getWindowHandles method returns a set of unique IDs. Those unique IDs are the IDs of windows returned in a set.

Set<String> windowHandles = driver.getWindowHandles();

Switching to a Specific Window

This method switches the WebDriver’s focus from the currently open browser window to the intended browser window. The targeted window’s id is passed as the argument to shift the web driver's control to the new window.

//we put a for loop and loop between the different window id and then switch to that particular window id and perform the operation which needs to be done in that window


String mainWindowHandle = driver.getWindowHandle();
for (String handle : windowHandles) {
    if (!handle.equals(mainWindowHandle)) {
        driver.switchTo().window(handle);
        break;
    }
}

Closing a Window

driver.close();

Here are some common window-related exceptions and how to handle them.

NoSuchWindowException

This exception is thrown when the specified window or tab is not found.

try {
    driver.switchTo().window("windowHandle");
} catch (NoSuchWindowException e) {
    // Handle the exception (e.g., logging, error reporting)
    e.printStackTrace();
}

Handling Frames

Frames, used to divide a web page into multiple sections, require specific handling in Selenium. We can utilize Selenium's methods to navigate through frames seamlessly.

Frames in a webpage refer to a feature that allows web developers to divide a browser window into multiple sections, each of which can display a separate HTML document. Each of these sections, known as frames, acts as an independent HTML document with its own content, allowing developers to create more complex and dynamic web layouts.

Frames are implemented using the <frame> or <iframe> HTML tags.

In order to switch to the frame, we need to call the switch-to-frame method. We can switch to frame by using frame ID or frame name.

Switching to a Frame by Index

// Switching to the first frame
driver.switchTo().frame(0);
 

Switching to a Frame by Name or ID

driver.switchTo().frame("frameNameOrId");

Switching Back to the Main Content

driver.switchTo().defaultContent();

Handling Nested Frames

When frames are nested within other frames, Selenium provides a way to navigate through them.

Switching to a Parent Frame

driver.switchTo().parentFrame();

Switching to a Frame by Web Element

We can also switch to the frame by using a web element reference as well.

WebElement frameElement = driver.findElement(By.id("frameId"));
driver.switchTo().frame(frameElement);

Here are some common exceptions related to frames and how to handle them.

NoSuchFrameException

Description: This exception is thrown when the specified frame is not found.

try {
    driver.switchTo().frame("frameName");
} catch (NoSuchFrameException e) {
    // Handle the exception (e.g., logging, error reporting)
    e.printStackTrace();
}

Summary

Effectively handling windows and frames is crucial for creating comprehensive and reliable test scripts with Selenium in Java. Elevate your automation scripts to new levels of efficiency and stability.

Happy learning.............!