Windows handling and switch Tab in selenium – online Training and Tutorials

Windows handling in selenium is one of the most crucial part to handle the different windows and to handle the different windows Tab. We may come across the various scenarios when we click on some link and another windows opened and we need to switch to the other window , to handle different windows while automation we use window handling concept in selenium and we also may come to scenarios to switch to different Tabs in selenium.

Note : Tabs are also windows opened in same window. Switching Tabs and Pop up windows is similar.


Windows handling in selenium and Handle Switching Tabs in selenium

To handle the multiple windows and Tabs in selenium we have below method defined in selenium.

driver.switchTo().window(Window_Id);

Above method will switch to windows with the given window id and this window id we have to fetch from another method.

Two methods are given in selenium to fetch the windows id.

  1. getWindowHandle() – This method will fetch the current window id only on which the driver is working currently.It will return the String – current window id.
  2. driver.getWindowHandles() – This method will fetch all the windows Id opened by the driver. It will return the SET<String> containing all opened windows id.

Lets have an exercise to understand switching of windows in selenium.

Below is the demo links.

Visit selenium online training link

  1. Launch the browser and open this page url.
  2. Print the title of this window.
  3. Click on the above link and it will open one new window.
  4. Switch to the new window.
  5. print the title of the new window.
  6. close the window.
  7. switch back to the main window ( this page window )
  8. Print the title of this window.

Java Program below 

package convert.Maven_Basics;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class WindowHandling_Tutorial {

	public static void main(String[] args) {

		// setting the driver driver path				
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver=new ChromeDriver();	//open browser	
		driver.manage().window().maximize(); // maximize the window
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.get("https://padhle.com/windows-handling-and-switch-tab-selenium/"); // Open URL
		// click on the demo link to open new window
		driver.findElement(By.partialLinkText("Visit selenium online training link")).click();
		// Get the Main Window Id into the String 
		String Main_Window = driver.getWindowHandle();
		// Fetch all the open windows into Set
		Set All_Window = driver.getWindowHandles();
		System.out.println(All_Window);
		// Creating Iterator to traverse the Sets 
		Iterator itr=All_Window.iterator();	
		// Traversing the Iterator
		while (itr.hasNext())
		{
			// Getting next value
			String Current_Window = itr.next();
			// Check if child window not equal to Main window
			if(!Current_Window.equalsIgnoreCase(Main_Window))
			{
				// Driver switch to child window
				driver.switchTo().window(Current_Window);
				System.out.println("I am in child window");
				System.out.println("Child Title is :- " + driver.getTitle());
				//output -> Child Title is :- Selenium Online Training
				//in Delhi , NCR , Rohini , Interview preparation
				driver.close();
			}
		}

		// Switch driver to Main Window
		driver.switchTo().window(Main_Window);
		System.out.println("I am in Main Window");	
		System.out.println("Parent Title is :- " + driver.getTitle());
		// output -> Parent Title is :- Windows handling and
		//switch Tab in selenium - online Training
	}
}

Output of above program 

I am in child window
Child Title is :- Selenium Online Training in Delhi , NCR , Rohini , Interview preparation
I am in Main Window
Parent Title is :- Windows handling and switch Tab in selenium - online Training

Note : To perform any action on the new window , we need to switch on that window first.


How to handle the more than two windows when we don’t have the window id? 

This is one of the important interview question of selenium and we will cover all interview question in our selenium online training.

To handle the multiple windows we can first switch to windows one by one and verify the title of the window and start performing action. we are just switching and verifying title of the window and then start performing action.

Note : This approach we will use when we more than 2 windows.

Exercise to switch windows in selenium

  1. Launch the browser and open this page url.
  2. Print the title of this window.
  3. Click on the above link and it will open one new window.
  4. Switch to the new window.
  5. verify the title ( if we are in correct window or not )
  6. print the title of the new window.
  7. close the window.
  8. switch back to the main window ( this page window )
  9. Print the title of this window.

Java Program to handle windows

package convert.Maven_Basics;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class WindowHandlingwithTitle {

	public static void main(String[] args) {
		// setting the driver driver path				
		System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
		WebDriver driver=new ChromeDriver();	//open browser	
		driver.manage().window().maximize(); // maximize the window
		// implicit wait is defined with max waiting time of 10 seconds
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.get("https://padhle.com/windows-handling-and-switch-tab-selenium/"); // Open URL
		// click on the demo link to open new window
		driver.findElement(By.partialLinkText("Visit selenium online training link")).click();
		// Get the Main Window Id into the String 
		String Main_Window = driver.getWindowHandle();
		// Fetch all the open windows into Set
		Set<String> All_Window = driver.getWindowHandles();
		System.out.println(All_Window);
		// Creating Iterator to traverse the Sets 
		Iterator<String> itr=All_Window.iterator();	
		// Traversing the Iterator
		while (itr.hasNext())
		{
			// Getting next value
			String Current_Window = itr.next();
			// Check if child window not equal to Main window
			if(!Current_Window.equalsIgnoreCase(Main_Window))
			{			
				{
					// Driver switch to child window
					driver.switchTo().window(Current_Window);
					// get window title 
					String Window_Title = driver.getTitle();
					System.out.println(Window_Title);
					// check whether window title is correct or not
					if(Window_Title.contains("Selenium Online Training"))
					{
						System.out.println("I am in child window");
						System.out.println("Child Title is :- " + driver.getTitle());
						driver.close();
					}
				}
			}
		}

		// Switch driver to Main Window
		driver.switchTo().window(Main_Window);
		System.out.println("I am in Main Window");	
		System.out.println("Parent Title is :- " + driver.getTitle());
	}
}

Output 

Selenium Online Training in Delhi , NCR , Rohini , Interview preparation
I am in child window
Child Title is :- Selenium Online Training in Delhi , NCR , Rohini , Interview preparation
I am in Main Window
Parent Title is :- Windows handling and switch Tab in selenium - online Training

This is all about the how to switch to windows or Tab in selenium. It covers all the selenium interview question of window handling topic. If you have doubts please mail us or mention in below comments.

Enroll for the Selenium online Training

Leave a Comment

Your email address will not be published. Required fields are marked *