Window Handling – Working With Multiple Windows

August 30, 2016

Selenium WebDriver has a provision to switch from one frame to another or to switch from one browser window to another in any web application. The intent of this article is to learn how to switch over multiple windows. Each browser window has a unique window handle assigned to it. This handle is used by selenium to uniquely identify a window.

Many a times we open a page in a new window (or tab), or sometimes by clicking on a link or on a button a new window (or new tab) opens up. The control of Selenium doesn’t automatically switches to this new window (or new tab), but we have to switch to perform any operation on this new window.

Lets consider a Scenario:

  • Navigate to http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open
  • Click Try it button.
  • A New Window opens up.
  • Print the title of this new window.
  • Close it and switch back to the old window.
  • Get the title of old window
  • Close the browser.

Here is the sample code:

CODE EXPLANATION- Few lines of code remains same i.e invoking browser, maximising window, deleting all cookies before actually launching your web URL. So, here we will start with the window handling part. Let’s go through each and every line of code-

Note: For simplicity, we are calling old window as Parent window and new one as child window.

  1. String parentWindow = Driver.getWindowHandle(); This line of code will return a String which is handle of parent window.
  2. System.out.println(“Parent window Id is : “+ parentWindow);- We are simply printing the String i.e. handle obtained from the parent window in our console.
  3. Driver.switchTo().frame(“iframeResult”); Refer the Screenshot below , we are switching from main window to a frame (as Try it button is in a frame.)
  4. Driver.findElement(By.tagName(“button”)).click();- We have located the “Try it” button by tagName locator.
  5. String childWindow;- Declaring a String variable called ‘childWindow’.
  6. childWindow = Driver.getWindowHandles().toArray()[1].toString();- This line of code is important, getWindowHandles() method returns a SET of unique handles of all the windows which are currently opened. To get the handle of child window, we converted that set into an array. Now as there are only two windows opened and child being the latest one, so it will be at first index. Convert the object into string which will return the unique handle of the new window.
  7. Driver.switchTo().window(childWindow);- Here we are switching to the child window.window() method takes String . Hence, we passed “childWindow” that we obtained in above code.
  8. System.out.println(“Title of child window is : “+ Driver.getTitle());- Printing the title of child window  using getTitle() method.
  9. Driver.close();- close() method closes the active browser window.Hence, child window would be closed.
  10. Driver.switchTo().window(parentWindow);-  Now, switching back the handle  to the parent window.
  11. System.out.println(“Title of Parent wind is : “+ Driver.getTitle());- Printing the title of parent window.
  12. Driver.quit();- quit() method closes all the browser windows  completely.

Window handling

This was all about Windows Handling using Selenium WebDriver.

PS: For any questions, queries or feedback. Feel free to write us at amrita@qatechhub.com orsupport@qatechhub.com. Happy Learning 🙂

Amrita Joshi

About the Author

Amrita Joshi

Leave a Comment: