How to Handle Multiple Windows In Selenium WebDriver

So far you have learned how to automate the objects of a web page after opening a URL. But what would happen if upon clicking on a button or a link it opens a new modal popup window, page in a new window, or a new tab. These are called the child window. By default, Selenium WebDriver does not identify the objects of the child window. If you simply try to automate the object of a child window you will get “NoSuchElementException“.In this tutorial, I will show you how to handle multiple windows in Selenium WebDriver.

Handle Multiple Windows in Selenium WebDriver

To demonstrate to you the scenario of handing multiple windows, we will open the W3School home page and click the button “Try It Yourself” to open a new window or child window and verify its heading.

In order to automate the above or such scenarios, we need to get the window handles of all child windows opened by Selenium WebDriver.

What is a Window Handle in Selenium WebDriver?

A Window Handle is a token that represents a resource that is managed by the Windows kernel. Anything that is open on a machine must have a window handle whether it is a browser, window application, or any file, etc. In simple words, you can consider it as a pointer to a window. Without a handle, Selenium can’t identify its child windows. The following screenshots demonstrate the same.

Handle Multiple Windows in Selenium WebDriver

Selenium does have methods to retrieve window handles. We will use the following methods to automate the aforementioned scenario.

  1. get.windowhandle(): It returns the window handle of the current window
  2. get.windowhandles(): It returns the window handles of all the windows opened by Selenium WebDriver
  3. set: The set method is used to set the window handles in the form of a string. set<string> set= driver.get.windowhandles()
  4. switch to: It is used to switch between the open windows

Example – Handling Multiple Windows in Selenium WebDriver

Test Scenario:

  1. Open W3School home page
  2. Store the parent window title in a variable using command String parent=driver.getWindowHandle();
  3. Click on the link “Try It Yourself”.It will open a New Tab (Child window). So now we will have two windows.
  4. Store the window handles of all windows using command Set<String> allWindowHandles = driver.getWindowHandles(); 
  5. Iterate through all window handles to find child window handles
  6. Print all Child window titles
  7. Verify the Title of the child windows.
  8. If the child window title matches the desired title, get its heading
  9. Print the retrieved heading in step 8
  10. Switch back to the parent window using the parent window handle
  11. Retrieve the heading of the parent window
  12. Print the heading retrieved at step 11

Output

Current Window Id is: CDwindow-5A489E5801B98C7E5F053EE9C90AB4C3
Current Window Id is: CDwindow-1B05460146A811FE074C556CE269584F
New Child Window Title: Tryit Editor v3.6
Child Page Heading: This is a Heading
Parent Page Heading: HTML

Conclusion

We can handle multiple windows in Selenium WebDriver.We can use getWindowHandle() and getWindowHandles() methods respectively to get the window handle of the current window as well as all windows opened by Selenium. We can switch to any specific child window by using its window handle with the help of the command driver.switchTo().window(WindowHandle);

Recommended Posts

 

 

 

 

 

 

 

 

 

 

 

Leave a Reply