Windows Handling | Multiple windows handling using JavascriptExecutor.

Traversing through multiple windows using ‘JavascriptExecutor’ and selecting user-defined tab window with ‘Title Partial Text’. This process is called multiple windows handling.

               Some times we have to open multiple windows (tabs) as per the requirement and need to switch the control through various tabs to select user defined tab window. For this, we can use partial text from the window title along with the switchTo statement.

Step to handle multiple windows:

  1. Launch the browser and open the main URL(
  2. Now set a keyword for the expected window using a String variable with partial window text on which user wants to navigate.
  3. Now launch five different tab windows with different URLs using JavascriptExecutor
  4. Use ‘Set collection‘ to get the list of all opened window at a time. (Note: A ‘Set collection’ contains no duplicate values in it.)
  5. Now traverse through each window using ‘foreach‘ loop and switchTo statement.
  6. compare the current window title with the expected window title.
  7. Print the complete Title of expected tab window.

Code Example:

import java.util.Set;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class HandlingMultipleWindow {

	public static void main(String[] args) throws InterruptedException 
		String Url_1="";
		String Url_2="";
		String Url_3="";
		String Url_4="";
		String Url_5="";
		// Initialze variable with partial text present in the title.
		// This would be case sensitive.
		String targetTabWindowTitle="Contact";
		System.setProperty("", "drivers\\chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		JavascriptExecutor jse= (JavascriptExecutor)driver;
		// Script to launch blank window.
		// jse.executeScript("'about:blank','_blank');");
		// Script to launch more windows with different URLs
		// Getting list of all opened windows
		Set multipleWindows= driver.getWindowHandles();
		// Use 'foreach()' loop to traverse through all windows
		for (String currentWindow : multipleWindows) 
			String currentWindowTitle=driver.getTitle();
		// Compare current window title with expected title.
				System.out.println("Current Window: "+currentWindowTitle);

Leave a Comment

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

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.