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

 

 

14 thoughts on “Handling Browser Window In Selenium Webdriver”

  1. Please let me know how we can achieve the below scenario.
    I want to navigate to 3rd window out of 5 windows. My doubt is as Set is an unordered collection, if we use iterator and iterate in for loop, how can we ensure that it is pointing to the required window?

    1. Ideally there should be a logic behind switching to required window. That may be a title or element. If you need to go by index, you can convert set in to list and can use get method. Internally it used LinkedHashSet. So it will be ordered only.

  2. HI Amod,
    Both webdriver instances are pointing to same or separate window?

    ANS
    Both webdriver instances are pointing to same or separate window because swicthTo method returns Webdriver after switching to new window which we are saving into another WebDriver instance.

    Can you please tell here whether its pointing to same window or separate. ?
    as in your answer its pointing to same or separate window is mentioned so little confusing.

  3. isstaed of using set we can use for loop and we can use arraylist to store window handles and then switch toi it.but which one is more helpful…

Leave a Reply

Your email address will not be published. Required fields are marked *