Switch Window Commands in Selenium WebDriver

HOME

                                      

One of the major testing workflow involves switching between multiple windows. Selenium WebDriver has specific switch commands to serve this purpose. Selenium WebDriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This unique alphanumeric id 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.

1) GetWindowHandle– To get the window handle of the current window. It returns a string of alphanumeric window handle.

String  parentHandle= driver.getWindowHandle();

2) GetWindowHandles– To get the window handle of all the windows. It returns a set of window handle.

Set  handle= driver.getWindowHandles();

3) SwitchTo Window – WebDriver supports moving between named windows using the “switchTo” method.

driver.switchTo().window("windowName");

OR
 
If we want to Iterate over every open window:-
for (String handle : driver.getWindowHandles()) {
 
    driver.switchTo().window(handle);}

Let us explain window switch with an example:-

1) Launch new Browser and open “https://www.seleniumeasy.com/test/window-popup-modal-demo.html”
2) Get current window handle and print it to console
3) Locate ‘Click to open a new browser window!’ button using Id
4) Click the button to open the new window
5) Get the window handles of both the open windows and print them
6) Switch to the new window with the handle’s reference
7) Get the title of Child Window and print it
8) Close the new window
9) Switch the control back to parent window and print the title of Parent Window
10) Close the parent window

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class PracticeSwitch {
            public static void main(String args[]) throws InterruptedException {
                        System.setProperty("webdriver.gecko.driver", "src\\test\\resources\\webdrivers\\window\\geckodriver.exe");
 
                        // Initiate Firefox browser
                        WebDriver driver = new FirefoxDriver();
 
                        // Maximize the browser
                        driver.manage().window().maximize();
                        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 
                        // Pass application url
                        driver.get("https://www.seleniumeasy.com/test/window-popup-modal-demo.html");
 
                        // Get all the details of the current window
                        String ParentHandle = driver.getWindowHandle();
                        System.out.println("Parent Window :" + ParentHandle);
 
                        //Find locator of "Follow on Twiteer" button and click 
                        driver.findElement(By.xpath("//a[@class='btn btn-primary followeasy']")).click();
 
                        // // Store and Print the name of all the windows open and Pass a window handle
                        // to the other window
                        for (String handle1 : driver.getWindowHandles()) {
                                    System.out.println("All Windows :" + handle1);
                                    driver.switchTo().window(handle1);
                        }
                        
                        Thread.sleep(4000);
                        System.out.println("Child_Title :" + driver.getTitle());
                        
                        // Close Child window
                        driver.close();
 
                        // Switch back to parent window
                        Thread.sleep(1000);
                        driver.switchTo().window(ParentHandle);
                        System.out.println("Parent Title :" + driver.getTitle());
 
                        // Close Parent window
                        driver.close();
            }
}

Output
Parent Window :21
All Windows :21
All Windows :6442450945
Child_Title :Selenium Easy (@seleniumeasy) / Twitter
Parent Title :Selenium Easy - Window Popup Modal Demo

Below is the example on how to handle more than one child window switch.
1) Launch new Browser and open “https://www.seleniumeasy.com/test/window-popup-modal-demo.html”
2) Get current window handle and print it to console
3) Locate ‘Follow All’ button using Id
4) Click the button to open the new window. It will open 3 windows
5) Get the window handles of all 4 open windows and print them
6) Change the URL of all 4 windows to “http.google.com”
7) Switch the control back to parent window and change the URL to https://www.mozilla.org/en-US/
8) Close all windows

import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
 
public class MultipleSwitchWindow {
            public static void main(String[] args) throws InterruptedException {
                        System.setProperty("webdriver.gecko.driver", "src\\test\\resources\\webdrivers\\window\\geckodriver.exe");
 
                        // Initiate Firefox browser
                        WebDriver driver = new FirefoxDriver();
 
                        // Maximize the browser
                        driver.manage().window().maximize();
                        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 
                        driver.get("https://www.seleniumeasy.com/test/window-popup-modal-demo.html");
 
                        // Get all the details of the current window
                        String ParentHandle = driver.getWindowHandle();
                        System.out.println("Parent Window :" + ParentHandle);
 
                        driver.findElement(By.id("followall")).click();
 
                        // Store and Print the name of all the windows open
                        Set AllHandles = driver.getWindowHandles();
                        System.out.println("Window Handle :" + AllHandles);
 
                        for (String handle : AllHandles) {
                                    System.out.println("Switching to window - > " + handle);
                                    System.out.println("Navigating to google.com");
 
                                    // Switch to the desired window first and then execute commands using driver
                                    driver.switchTo().window(handle);
                                    driver.get("http://google.com");
 
                                    Thread.sleep(1000);
                        }
 
                        // Move back to Parent Window
                        System.out.println("Switching to Parent window - > " + ParentHandle);
                        driver.switchTo().window(ParentHandle);
                        System.out.println("Navigating to Mozilla");
                        driver.get("https://www.mozilla.org/en-US/");
 
                        // Close all the browsers opened
                        driver.quit();
            }
}

Output
Parent Window :18
Window Handle :[18, 4294967297, 4294967298, 4294967299]
Switching to window - > 18
Navigating to google.com
Switching to window - > 4294967297
Navigating to google.com
Switching to window - > 4294967298
Navigating to google.com
Switching to window - > 4294967299
Navigating to google.com
Switching to Parent window - > 18
Navigating to Mozilla

4 thoughts on “Switch Window Commands in Selenium WebDriver

  1. I'm new to Selenium and learning. I am trying to automate Facebook login page and I get a dialog box or alert or new window, not sure. Can you suggest how to automate that.

    Like

  2. When we automate Facebook login page, it has a child window. To automate that page, move cursor to child window and click on \”Accept All\” button. Code will look like belowdriver.get(\”https://www.facebook.com/\”); Thread.sleep(1000); // switch from main window to child window for (String handle1 : driver.getWindowHandles()) { driver.switchTo().window(handle1); } driver.findElement(By.id(\”u_0_k\”)).click();

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s