Handling Browser Window In Selenium Webdriver

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

 

 

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw 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