Make Selenium Easy

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

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<String>. 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:-

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

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Table of Contents

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

2 thoughts on “Selenium Interview Question 9 – Difference Between getWindowhandle() and getWindowHandles()

  1. How to handle below scenario-

    If there are 4 windows get open and wanted to switch only on specific window and wanted to perform operation, after getting all windows handle how we can identify that this window id is belong to this particular window

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

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

by Amod Mahajan time to read: 2 min
%d bloggers like this: