Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Selenium Interview Question 9 – Difference Between getWindowhandle() and getWindowHandles()

Posted on 01/13/2025 By admin

As a part of Selenium Interview Questions series, in this post, we will learn differences between getWindowHandle() and getWindowHandles() in Selenium WebDriver.

A window handle is a unique identifier that is assigned to each window created. Selenium WebDriver provides you two methods getWindowHandle() and getWindowHandles() which are used to get window handle/s.

Differences between both are given below:-

  1. getWindowHandle() returns the window handle of currently focused window/tab. getWindowHandles() returns all windows/tabs handles launched/opened by same driver instance including all parent and child window.
  2. Return type of getWindowHandle() is String while return type of getWindowHandles() is Set. The return type is Set as window handle is always unique.
  3. In chrome and Firefox , Each tab in a window will have unique window handles. So getWindowHandles() will return handles for all tabs of a window. For example:- If there are four tabs in a window is opened, getWindowHandles() method will give “four” for chrome and firefox browsers. I am not sure about IE and EDGE. I will bring this in a new post.
  4. getWindowHandles() internally uses LinkedHashSet. So whatever Set it returns, it will give window handles in order it is opened.

Some important points where interviewer may confuse you:-

  1. As soon as you launch a browser window, it gets its window handle. It is not mandatory to load a URL in to it.
  2. If you close browser and then call getWidnowHandle() or getWindowHandles() on driver, you will get NoSuchSessionException stating “Session ID is null. Using WebDriver after calling quit()?“.

Example Program:-

package FeaturesExamples;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.github.bonigarcia.wdm.WebDriverManager;

public class WindowHandles {
        public static void main(String[] args) {

                WebDriverManager.chromedriver().setup();
                WebDriver driver = new ChromeDriver();
                // I just launched browser. Did not load any URL
                System.out.println(driver.getWindowHandle());
                // Quitting browser
                driver.quit();
                // Calling get window handle method
                System.out.println(driver.getWindowHandles().size());
        }
}

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

#HappyLearning

Uncategorized

Post navigation

Previous Post: Handling Browser Window In Selenium Webdriver
Next Post: apply method in Function

Related Posts

How To Locate Web Element Which Has Multiple Class Names Uncategorized
API Testing Tutorial Part 6 – Idempotent Methods in HTTP Methods Uncategorized
April 30, 2018 – Make Selenium Easy Uncategorized
Are Your Selenium Scripts Ready For Parallel Execution? Uncategorized
How To Solve – IllegalArgumentException: Keys to send should be a not null CharSequence Uncategorized
patch 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