In this post we will learn how to work with windows/tabs while we are doing web automation with selenium webdriver.


This post is designed for SW test professionals who have some information about selenium web automation. At the end of this tutorial, you will know how to work with windows/tabs.


Nice to have: Before starting to read this post it is better to read previous selenium tutorials.

Working with Window Handles

Every window has a unique handle. Handle means a specific identifier that represents its window. We can manage the windows with their handles. I want to explain what Selenium Webdriver provides us for window handles.

– driver.getWindowHandle() – We can get current window’s handle

– driver.getWindowHandles() – We can get all windows handles

– driver.switchTo().window(String handle) – We can switch to the target window by using its handle.

Example-1: Window Handles

Test website: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_link_target

Test Scenario:

  1. Go to above URL.
  2. Get the current window’s handle and write to the console window. It must be first window handle.
  3. Locate the “Visit W3Schools.com!” link and click it.
  4. Get all window handles and hold them in a list.
  5. Write to total window handle number to the console. It must be 2.
  6. Switch to second window.
  7. Get the current window’s handle and write to the console window. It must be second window handle.
  8. Check the upper left side text is “THE WORLD’S LARGEST WEB DEVELOPER SITE” in second window.
  9. Go back (Switch) to first window.
  10. Get the current window’s handle and write to the console window. It must be first window handle.
  11. Check the See Result Button Text. It must contain “See Result” text.

Test Code:

Console Output:


How to Manage Windows

Webdriver provides us below methods to manage windows. We can control the size and position of the current window with the following methods.


– .maximize() – It maximizes the current window.

– .getSize() – It returns the size of the current window.

– .setSize() – It sets a new size to current window.

– .getPosition() – It returns current position in term of x,y coordinates.

– .setPosition() – It moves the current window around.

Example-2: Manage Window

Test website: http://www.swtestacademy.com

Test Scenario:

  1. Navigate to above URL.
  2. Maximize the current window.
  3. Get size of the window and write the full screen size to the console.
  4. Minimize the window by 1/4 and write the new screen size to the console.
  5. Get window position and write it to the console.
  6. Set window position x=100 and y=200 and write to the console.

Test Code:

Console Output:



  • You learnt how to handle and manage browser windows with selenium webdriver methods.