Handling Windows and Frames using Selenium With Python

Introduction

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

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

window_handles method returns a set of unique id. Those unique id are id of windows returned in a set.

window_handles = driver.window_handles

Switching to a Specific Window

Switch to 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 WebDriver’s control to the new window

# Assuming the main window is the first handle
main_window_handle = driver.window_handles[0] 


# 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
for handle in window_handles:
    if handle != main_window_handle:
        driver.switch_to.window(handle)

Closing a Window

To close the window use the below command

driver.close()

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 switch to frame method. We can switch to frame by using frame id or frame name.

Switching to a Frame by Index

driver.switch_to.frame(0)  # Switching to the first frame

Switching to a Frame by Name or ID

driver.switch_to.frame("frameNameOrId")

Switching Back to the Main Content

driver.switch_to.default_content()

Handling Nested Frames

In cases where frames are nested within other frames, Selenium provides methods to navigate through them.

Switching to a Parent Frame

driver.switch_to.parent_frame()

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

Switching to a Frame by Web Element

frame_element = driver.find_element(By.ID, "frameId")
driver.switch_to.frame(frame_element)

Summary

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

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


Recommended Free Ebook
Similar Articles