Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Handling Browser Window In Selenium Webdriver

Posted on 01/13/2025 By admin

Hello Folks,

In this post, we will learn “Handling browser windows in Selenium webdriver.”.

Consider an example. You are browsing a website and when you click on a link, it opens up a new browser window and display web page contains. Manually, you need to switch to new browser window to perform some actions. The same you need to instruct selenium webdriver to switch to required window because by default selenium webdriver points to first window opened.

Understanding what is window handle?
If you go by literal meaning of English word “handle”, it is same here as well. Consider a traveler bag. It has a handle. Using that handle you can operate bag easily. One handle is associated with single bag. So, every bag will have their own handle. The same concept is applied for window handle.

Every browser has one handle which is used by Selenium webdriver to control that browser. Selenium webdriver provides a method named getWindowHandle to get the handle of current window.

getWindowHandle():

  • It returns an opaque handle to this window that uniquely identifies it within this driver instance.
  • We can switch to any window using its unique window handle.
  • Window handle is an alphanumeric.
  • Return type of getWindowHandle() is a String type.

We can see window handle of a browser by running below java program:

Output:
Window handle: CDwindow-3929e9db-61ab-4eca-8cf7-6cbaebd6c17e

getWindowHandles():

  • It return a Set of window handles open by particular driver instance.
  • It returns a Set because handle of each window is unique and Set allows only unique elements.
  • Using this method, we can iterate over any window open by current instance.

See an example to understand getWindowHandles:

Output:

You can see window handle for both windows are different.

Using this window handle we can switch to respective window. Without switching to new window, we can not perform any action on that window. It is always a best practice to switch and verify title of window in case of multiple windows. This is interview question as well.

See an example below:

Output:

Interview question:

I want to share an interview question here as well. See below code snippet carefully:

Both webdriver instances are pointing to same or separate window?

Answer: Both webdriver instances are pointing to same window because switchTo method returns a Webdriver instance after switching to new window which we are storing into another WebDriver instance.

That’s it guys. If you have any doubt, feel free to comment below. If you like my posts, please like, comment, share and subscribe. #ThanksForReading

#HappySelenium

Uncategorized

Post navigation

Previous Post: Git Tutorial 11 – Git Pull – Download And Merge Changes From Remote Repository
Next Post: Selenium Interview Question 9 – Difference Between getWindowhandle() and getWindowHandles()

Related Posts

Stream api in java Uncategorized
Untitled Diagram (6) – Make Selenium Easy Uncategorized
Taking Screenshot Using TakesScreenshot Interface In Selenium Uncategorized
July 7, 2017 – Make Selenium Easy Uncategorized
parameterised dataprovider Uncategorized
Frequently Asked Java Programs 31 – Java Program to Find Common Characters in Two Given Strings Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark