Selenium WebDriver Switch Window Commands

Some web applications have many frames or multiple windows. Selenium WebDriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This unique alphanumeric id is called window handle. Selenium uses this unique id to switch control among several windows. In simple terms, each unique window has a unique ID, so that Selenium can differentiate when it is switching controls from one window to the other.

 

GetWindowHandle Command

Purpose: To get the window handle of the current window.

 

GetWindowHandles Command

Purpose: To get the window handle of all the current windows.

 

SwitchTo Window Command

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

Or

Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:

Or

Switching between windows with Iterators:

 

SwitchTo Frame Command

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

 

SwitchTo PopUp Command

Purpose: WebDriver supports moving between named PopUps using the “switchTo” method. After you’ve triggered an action that opens a popup, you can access the alert and it will return the currently open alert object. With this object you can now accept, dismiss, read its contents or even type into a prompt. This interface works equally well on alerts, confirms, and prompts.

 

Practice Exercise 1

1) Launch new Browser

2) Open URL “http://toolsqa.wpengine.com/automation-practice-switch-windows/”

3) Get Window name (Use GetWindowHandle command)

4) Click on Button “New Message Window”, it will open a Pop Up Window

5) Get all the Windows name ( Use GetWindowHandles command)

6) Close the Pop Up Window (Use Switch Command to shift window)

Solution

 

 

Practice Exercise 2

1) Launch new Browser

2) Open URL “http://toolsqa.wpengine.com/automation-practice-switch-windows/”

3) Click on Button “Alert Box”, it will open a Pop Up Window generated by JavaScript

4) Switch to Alert window (Use ‘SwitchTo()Alert() command)

5) Close the Pop Up Window (Use Accept command)

 

navigate-left-iconnavigate-right-icon