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.
#ThanksForReading
#HappyLearning

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

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

3 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

    1. after getting all window handles, if you want to perform any operation in any of the window page then take for loop and within that write an if condition with expvalue.equalsignorecase(actvalue). if condition is satisfied then perform the necessary operations you want to perform .
      Here exp value should be the windowid of the specific page where u want to perform operations.

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
3
%d bloggers like this: