How To Handle Multiple Windows In Selenium?

How To Handle Multiple Windows In Selenium?

how to handle multiple windows in selenium

How To Handle Multiple Windows In Selenium?

How To Handle Multiple Windows In Selenium?

How To Handle Multiple Windows In Selenium?


How To Handle Multiple Windows In Selenium?

How To Handle Multiple Windows In Selenium

As a web application tester, you’ve probably come across a typical situation while automating – when you click a button, several windows pop up, and you have to do things in each of them. But dealing with all these windows at once can be tough. That’s where Selenium steps in to help!

This blog covers “How to handle multiple windows in Selenium.” We’ll also explain Selenium windows using simple examples. Read the complete blog to get how it works. 

So Let’s First Understand What Is Windows In Selenium?


Enroll now and take the first step towards a successful career. Click here to join our courses today!


What Is Windows In Selenium?

In Selenium Automation Testing, when we talk about “windows”, we’re referring to the different browser windows or tabs that can open up when you interact with a web application. These windows can be the primary browser window or ones that arise after clicking links or buttons.

There are two types of Windows In Selenium 

  1. Main Window
  2. Child Window

Let’s understand these types one by one with simplest examples

1) Main Window

The main window is the initial browser window that opens when you start a Selenium test. It’s like the front door of a house. You interact with the elements on this window as you navigate through the web application.

For Example:

Let’s say you’re testing an online shopping website. The main window is where you land when you open the website. You can click on different products, add them to your cart, and navigate through various pages – all within this main window.


2) Child Window

Child windows, on the other hand, are additional windows that pop up when you perform certain actions. These could be like opening new doors within the house that lead to different rooms.

For Example:

Back to the online shopping example, imagine you’re looking at a product and you click a “Details” button. A new window might pop up with more information about the product. This is a child window. You can interact with elements in this window, like reading the product description, without losing track of the main window.


What Is Windows Handle In Selenium?

A window handle is like an address for browser windows. It’s a unique code made of letters and numbers that helps Selenium identify and manage windows. Just as a key opens a specific room in a building, a window handle lets Selenium switch between different windows. Each window has its own handle, making it easy to work with multiple windows and their child windows while testing a website.


What are Methods To Handle Windows In Selenium? 

1) getWindowHandle(): This method returns the unique handle of the currently focused window. It’s often used to identify the main or parent window.

2) getWindowHandles(): This method retrieves the handles of all open windows. It returns a set of strings, each representing a unique window handle.

3) switchTo().window(handle): This method allows you to switch your focus between different windows. You provide the window handle as an argument, and Selenium switches your interaction context to that window.

4) close(): This method closes the currently focused window. If it’s the only window open, it may also terminate the browser session.

5) quit(): This method closes all windows and terminates the browser session, ending the WebDriver instance.


Why do we need to handle multiple windows in Selenium?

1) Popup Windows and Dialogs: Some websites open popup windows for actions like login, notifications, or additional information. Selenium needs to handle these pop ups to interact with their elements.

2) Multiple Page Flows: Web apps often use multiple windows to guide users through different tasks or processes. Selenium needs to navigate and test across these windows seamlessly.

3) External Links: Clicking on links that open in new windows or tabs requires Selenium to switch to the new context to validate the content.

4) User Interactions: Simulating user behavior often involves opening new windows, like filling out a form in a popup. Selenium must handle these interactions for end-to-end testing.

5) Third-Party Integrations: Applications may integrate with third-party services in new windows. Selenium must verify these integrations function correctly.

6) User Scenarios: Testing user workflows sometimes requires interactions across multiple windows, like selecting a product and then viewing its details in a new window.

7) Consistency and Flow: Selenium ensures smooth user experiences by validating that windows open, close, and interact as expected.


Level up your skills in Software testing. Enroll now and become an expert. 


How To Handle Multiple Windows In Selenium

Handling multiple windows in Selenium involves a step-by-step approach to switch between windows, interact with their elements, and ensure a seamless testing process. 

Here’s a step by step guide on how to handle multiple windows in Selenium:

Step 1: Open Main Window

from selenium import webdriver

Step 2: Trigger Action for New Window

Step 3: Capture Window Handles

Step 4: Switch to New Window

Step 5: Do Stuff in the New Window

Step 6: Switch Back to Main Window

Step 7: Continue Testing on Main Window

Step 8: Repeat as Needed

Step 9: Close or Quit

This is the Full Code Example:


How to close all windows while handling multiple windows in Selenium?

Step 1: Capture All Window Handles:

Retrieve the handles of all open windows using driver.window_handles.

Step 2: Iterate and Close:

Loop through each window handle and switch to that window using driver.switch_to.window(handle). Then, use driver.close() to close the window.

Step 3: Quit the Browser Session:

After closing all windows, use driver.quit() to terminate the entire browser session.

Here’s a detailed example:

In this example, we open two new windows using different buttons, capture their handles, and then iterate through each handle to close the windows. Finally, we quit the browser session to ensure everything is properly closed.


Also Read : What Is Software Testing



In conclusion, gaining expertise in handling multiple windows in Selenium is important for ensuring thorough and effective automated testing of web applications. By smoothly switching between different windows, like pop-ups, new tabs, or separate browser instances, testers can simulate real-world user scenarios and thoroughly validate the application’s functionality.

I hope you had a good time reading this blog about how to handle multiple windows in Selenium. It should now be easier for you to work with multiple windows in your Selenium projects.



How to click twice in Selenium?

To click an element twice in Selenium, you can use the .click() method twice consecutively on the same element. 

For Example:


How To Handle Two Frames In Selenium?

To handle two frames in Selenium:

  1. Use driver.switch_to.frame(frame_name_or_id) to switch between frames.
  2. After interactions, switch back to the default content with driver.switch_to.default_content().


Which method is used to work with multiple browser windows in Selenium?

The method used to work with multiple browser windows in Selenium is driver.window_handles to capture handles, and driver.switch_to.window(handle) to switch between windows.


Ready to become a Software Tester?

Enroll now at Technogeeks to secure your future!

Get Free Career Counseling +91 8600998107 Or +91 7028710777 



Leave a Reply

Your email address will not be published. Required fields are marked *

Blogs You May Like

Get in touch to claim Best Available Discounts.

If You Are Looking for Job Assistance Please Fill Up the Form.

× How can I help you?