Handle Multiple Windows using selenium:

 

  • Selenium WebDriver provides an alphanumeric ID for each window as soon as the WebDriver object is given instantiated.
  • This unique alphanumeric ID is called window handle id in Selenium.
  • This unique ID uses to switch control between multiple windows.
  • In simple words, each unique window has a unique ID, so that selenium can differentiate when it is switching from one window to another.

 

GetWindowHandle Command:

Purpose: To get the window handle of the current window. Store current window ID into String object.

  • When we handle to Window first time we have need to store current window because of after perform any operation we need to switch back into parent window.

 

GetWindowHandles Command:

Purpose: To get the window handle of all the currently open windows.

  • Store all currently open windows into String Set. Store all window’s to perform operations on different tab’s at a time.

 

SwitchTo Window Command:

Purpose: WebDriver supports moving between named windows using the “switchTo” method.

  • Switch WebDriver control between one Window to Another. To perform operations on windows we have need to switch between multiple tabs.

Or

Alternatively, you can pass a “window handle” to the “switchTo().window()” method to switch between multiple window’s. It is possible to iterate over every open window like this:

SwitchTo Frame Command:

Purpose:  WebDriver can be move between named frames using the “switchTo” method from one frame to other. Go through in Details SwitchToFrame click on this Link:

 

Scenarios of Switch one window to other window:

  • Current window location store into Parent Window string.
  • Click some link that opens a new window.
  • Store newly open window into Child Window using getWindowHandles.
  • Switch to Newly open window using switchTo().
  • Code to do something on new window.
  • Close newly opened window when done with it.
  • switch back to the Parent window.

 

Example of Handle Newly open Window:

Handle Multiple Windows using selenium