How To Handle Multiple Windows In Selenium WebDriver Using Java?

Ria Dayal

Posted On: March 24, 2022

view count102020 Views

Read time12 Min Read

When automating any website or a web application in Selenium, you might have come across a scenario where multiple windows open within an application when a button is clicked, and appropriate action needs to be performed on the opened windows. Alas, you might not be in a position to work on all windows at the same time. Hence there is a need for some mechanism through which you can gain control over the parent and child windows.

In this Selenium Java tutorial, we will understand what Windows are and how to handle multiple windows in Selenium WebDriver using Java.
What is a Window in Selenium?

Starting your journey with Selenium WebDriver? Check out this step-by-step guide to perform Automation testing using Selenium WebDriver.

Watch this video to learn how to handle multiple windows in Selenium WebDriver using Java.

Let’s get started!!

What is a Window 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.

Check out this video to learn about Windows in Selenium:

For example, when you open any website or link, the page you land in is the Main Window.
Here, I am logging into the Selenium Playground offered by LambdaTest, and the below-shown window is the Main Window.

Multiple Windows in Selenium

LambdaTest is a cloud-based cross browser testing platform that supports Selenium Grid, providing a solution to every obstacle you face while performing automation testing on your local machine. Automation testing tools like LambdaTest offer a cloud-based Selenium Grid consisting of an online device farm of 3000+ real browsers and operating systems for you to perform Selenium automation testing at scale.

How to identify parent and child windows in Selenium?

As discussed in the above section of this blog on how to handle multiple windows in Selenium WebDriver using Java, 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.

Now, all the other windows that open inside your main window are termed as Child Windows. In order to understand this better, let’s log in to the Selenium Playground and use the Window Popup Model option.

Once you click on the Window Popup Model option, the page you land in is the Parent Window, and the window which opens by clicking on Follow On Twitter option is the Child Window.

Selenium Multiple Windows Handling

It is important to note that there can be single or multiple child windows inside your parent window.

What is Window Handle in Selenium?

Now that you have understood the concept of Windows in Selenium let’s look at how to control these different browser windows. This is where the Window Handle comes into the picture.

A window handle stores the unique address of the browser windows. It is just a pointer to a window whose return type is alphanumeric. The window handle in Selenium helps in handling multiple windows and child windows. Each browser window has a unique window handle value through which it can be identified.

In the previous section of this article on how to handle multiple windows in Selenium WebDriver using Java, when a new window was opened by clicking on the Follow On Twitter option, it had its Window Handle (or ID). This helps to switch the Selenium WebDriver context to the corresponding window and perform any operation.

Note: Selenium WebDriver 4 is a major architectural update in Selenium 4. WebDriver is the de facto standard for automating web browsers, enabling you to run automated tests on a variety of browsers. Follow along with this video to learn what’s new in Selenium 4.

You can follow the LambdaTest YouTube Channel and stay updated with the latest tutorials around Selenium testing, Cypress E2E testing, CI/CD, and more.

What are the different methods used for window handling in Selenium?

Let’s see what are the different methods through which we can handle these windows in Selenium. Selenium WebDriver provides us with various methods for handling windows. Below are a few of them:

  • 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.

Also readHow To Switch Between iFrames In Selenium Java [Tutorial]

How to handle a single child window in Selenium?

Let us now implement a code snippet for handling the single-child window in Selenium.

Use Case

  1. Log in to the Window Popup Modal Demo page of Selenium Playground.
  2. Click on Follow On Twitter option in the Single Window Popup section
  3. Switch to the child window opened
  4. Print the header of the web page.

Multiple Windows in Testing

Implementation

We will implement the case using Selenium with Java and use the cloud Selenium grid offered by LambdaTest for executing our test case.

Selenium Grid refers to a software testing setup that enables QAs to perform parallel tests across multiple browsers and devices with unique operating systems. When the entire setup of Selenium Grid is accessible using cloud-based servers, it is called Selenium Grid on Cloud. An online Selenium Grid helps you focus on writing better Selenium test scripts rather than worrying about infrastructure maintenance.

MultipleWindowsInSelenium.java

You can use the below testng.xml file for running the above testcase.

testng.xml

And the below pom.xml file for installing all the necessary dependencies.

pom.xml

Code Walkthrough

Let us now see the different areas of code in detail.

  1. Imported Dependencies: Here, we have imported all the necessary classes of Selenium WebDriver, WebDriverWait, Desired Capabilities, and RemoteWebDriver to set the respective browser capabilities and run Selenium IDE tests on cloud grid.
  2. Handling Multiple Windows in Selenium

  3. Global Variables: As we have used a Selenium Grid Cloud like LamdaTest to perform our test execution, we are using the below-shown variables.
  4. Here, you can populate the values for your corresponding username and access key, which can be collected by logging into your LambdaTest Profile Section. You can copy the Username and the Access Token to be used in the code. However, the grid URL will remain the same, as shown below.

    Selenium Multiple Windows Handling

    We have also used the Java Listener class here in order to customize the TestNG Report. TestNG provides us with a lot of Listeners (e.g., IAnnotationTransformer, IReporter, etc.). These interfaces are used while performing Selenium automation testing mainly to generate logs and customize the TestNG reports.

    If you want to know more about Event Listeners In Selenium WebDriver watch this video to learn how the Listeners “listen” to the event defined in the Selenium script and behave accordingly.

    To implement the Listener class, you can simply add an annotation in your test class just above your class name.

    Syntax:

    Also read: TestNG Listeners In Selenium WebDriver With Examples

  5. @BeforeTest(Setup Method): Here, we have used the LambdaTest Desired Capabilities Generator and have set the necessary capabilities of browser name, version, platform, etc., for our Selenium Remote WebDriver. After that, we are opening the Window Popup Demo Page in the launched browser.
  6. Handling Many Windows in Selenium

  7. @Test(singleWindowPopup): Here, we are first locating the Web Element for Follow On Twitter option and clicking on it.
  8. After this, the driver.getWindowHandle() helps in getting the unique identifier of the main window and storing it in a string.

    Selenium Multiple Windows Handling

    In the next step, we store the handles of all the opened windows in a Set of String.

    Multiple Windows in Selenium

    Now, we start iterating over the set of window handles we have by using the driver.switchTo() method, we switch to the child window. After switching to the child window, we print the web page title and close the same.

    Testing Many Windows in Selenium

  9. @AfterTest(closeBrowser): Here, we are just closing the launched browser.

Testing for Multiple Windows in Selenium

Once the tests are completed, you can also view your test results, logs, and the test recording as well in your LambdaTest Automation Dashboard.

Selenium Automation for Multiple Windows

With LambdaTest, you can see how your automated browser testing efforts are going. You’ll get real-time reports on the performance of your test suites and an automated system to track change impact across all your automation efforts. You can use LambdaTest Analytics to get insight into this information and drive better testing practices.

Multiple Windows in Selenium Automation

Console Output

Once you run the test case, the console output will look something like below:

Handle Many Windows in Selenium

Also readHow To Handle Login Pop-up In Selenium WebDriver Using Java?

How to handle multiple child windows in Selenium?

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.

Let us write our script for handling one of such scenarios.

Use Case

  1. Log in to the Window Popup Modal Demo page of Selenium Playground.
  2. Click on Follow On Twitter and Facebook in the Multiple Window Popup section.
  3. Switch to each of the child windows opened.
  4. Click on the Sign Up button for Facebook Window.
  5. Click on the Sign Up button for Twitter Window.

Implementation

MultipleWindowsInSelenium.java

Code Walkthrough

In this case, the process of iterating the child windows is exactly the same as how we did it in the case of a single child window. However, here we are distinguishing the windows respective to Facebook and Twitter by making use of their corresponding titles and by making use of if-else statements, we are clicking on their respective Sign Up buttons.

Multiple Windows in Testautomation

Console Output

Once you run the test case, the console output will look something like below:

Multiple Windows in Selenium Testautomation

You can also refer to the below video tutorial on how to handle Windows and Frames in selenium.

How to switch back from child window to parent window in Selenium?

You saw how we switch to a child window from the 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.

Use Case

Let us see how to implement the switchTo() function. We will implement the same use case as discussed in the last section of this article on how to handle multiple windows in Selenium WebDriver using Java.

Implementation

MultipleWindowsInSelenium.java

Also read: FindElement And FindElements In Selenium [Differences]

Code Walkthrough

In this test case, after navigating to the child window and closing it. We specifically switch back to the parent window and close that too, and as a result, we don’t need to close the browser again after the test.

Multiple Windows Testing

Console Output

Once you run the test case, the console output will look something like below:

Testing for Multiple Windows

Also readHow To Minimize Browsers In Selenium WebDriver Using JUnit

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

While we are handling multiple windows in Selenium, it is very important to understand how you can actually close the windows.

As you saw in the above example, in order to close the parent window, we had to explicitly switch to the parent window and then close it using the driver.close() method. The driver.close() method helps us to close the driver, which is being controlled at the moment. But what if we want to close all the opened windows at once?

In such cases, the method which can come in handy is the driver.quit(). The driver.quit() method closes all the open windows regardless of where the control is lying.

So now you know what to use when you want to tackle all the open windows at once! 🙂

This certification demonstrates your knowledge of Selenium and Java, and your expertise at automating tests for any project.

Here’s a short glimpse of the Selenium Java 101 certification from LambdaTest:

Conclusion

In this article on how to handle multiple windows in Selenium WebDriver using Java, we read about Windows in Selenium. We saw what are Parent and Child Windows and how we can handle them using their corresponding Window Handles. We also implemented the automation script for handling both single and multiple child windows. Additionally, we learned about switching back to the parent window, and the trick of closing all the opened windows at once.

I hope you enjoyed reading this article on how to handle multiple windows in Selenium WebDriver using Java, and it helps you in handling multiple windows for your Selenium automation 🙂

Happy Testing !!

Frequently Asked Questions (FAQs)

Which method is used to work with multiple browser windows?

Selenium Grid is a tool that allows you to run tests across multiple browsers, operating systems, and machines. The tool makes it possible to eliminate certain kinds of dependencies between test cases, thus making your testing framework more resilient.

How do I close the only child window in Selenium WebDriver?

The Selenium supports multiple window handling with the getWindowHandles method. The getWindowHandles method stores all opened window handles in the Set object and can be accessed by the getWindowHandle() method.

Author Profile Author Profile Author Profile

Author’s Profile

Ria Dayal

A Senior Quality Engineer By Profession, an automation enthusiast and loves to anchor. Her expertise revolves around Selenium, Java, Rest Assured, and Jenkins. Shell scripting too interests her a lot. Ria enjoys reading novels and writing is her comfort zone.

Blogs: 9



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free