logo

The best IT Trainig Institute In Gurgaon

Handle Multiple Windows

Handling multiple windows in Selenium involves managing different browser windows or tabs that are opened during a test session. Selenium WebDriver provides mechanisms to switch between these windows, allowing interactions in each one. Here’s a comprehensive guide on how to handle multiple windows in Selenium:

Key Concepts

1. Window Handle:

  • A window handle is a unique identifier assigned to each browser window or tab.
  • Selenium uses these handles to switch between different windows.

2. Switching Windows:

    driver.switchTo().window(windowHandle):
  • Switches the context of the WebDriver to the specified window handle, allowing you to interact with elements within that window.
Common Steps in Handling Multiple Windows:

1. Identify and Store Window Handles:

    driver.getWindowHandles()
  • to get a set of all window handles opened by the WebDriver. This method returns a set of strings, each representing a unique window handle.

2. Switching Context:

  • Once you have the window handles, you can use
    driver.switchTo().window(windowHandle)
    to switch the WebDriver’s context to the desired window or tab.

3. Perform Actions:

  • After switching to a particular window, you can interact with the elements in that window (e.g., fill out forms, click buttons).

4. Switching Back:

  • You may need to switch back to the original window or move to another window as your test requires.
Coding of handling Multiple Windows
package asc;

import java.util.Iterator;
import java.util.Set;

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

import io.github.bonigarcia.wdm.WebDriverManager;

public class MultipleWindows {

public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();

driver.get("https://www.salesforce.com/in/?ir=1");

driver.manage().window().maximize();

driver.findElement(By.xpath("//*[@id=\"1682325589592_4ma\"]/div[1]/article/div[2]/div/pbc-button[1]")).click();

Thread.sleep(4000);

Set windowhandles = driver.getWindowHandles();
System.out.println(windowhandles);

Iterator iterator =  windowhandles.iterator();

String parentwindow = iterator.next();
System.out.println(parentwindow);
String childwindow = iterator.next();
System.out.println(childwindow);

driver.switchTo().window(childwindow);
Thread.sleep(4000);

driver.findElement(By.name("UserFirstName")).sendKeys("Akhil");

Thread.sleep(4000);

driver.findElement(By.name("UserLastName")).sendKeys("Chukkalwar");

driver.switchTo().window(parentwindow);
		

}

}
code
Output
output output output
Code Breakdown and Explanation:

1. Setup and Navigation:

    
    WebDriverManager.chromedriver().setup();
    ChromeDriver driver = new ChromeDriver();
    driver.get("https://www.salesforce.com/in/?ir=1");
    driver.manage().window().maximize();
        
    WebDriverManager.chromedriver().setup();
  • Sets up the ChromeDriver using WebDriverManager.
  • new ChromeDriver();
  • Initializes a new ChromeDriver instance.
  • driver.get(...);
  • Opens the specified URL.
  • driver.manage().window().maximize();
  • Maximizes the browser window.

2. Interacting with the Page:

    driver.findElement(By.xpath("//*[@id=\"1682325589592_4ma\"]/div[1]/article/div[2]/div/pbc-button[1]")).click();
            
  • Finds and clicks a button or link on the page, which opens a new window or tab. The element is located using an XPath expression.

3. Handling Multiple Windows:

    
    Thread.sleep(4000);
    Set windowhandles = driver.getWindowHandles();
    System.out.println(windowhandles);
            
    Thread.sleep(4000);
  • Waits for 4 seconds to allow the new window to load. This is a simple (but not recommended for robust automation) way to wait for elements to become available.
  • driver.getWindowHandles();
  • Retrieves a set of window handles, which are unique identifiers for each window currently opened by the WebDriver.
  • System.out.println(windowhandles);
  • Prints the window handles to the console.

4. Switching Between Windows:

                
    Iterator iterator = windowhandles.iterator();
    String parentwindow = iterator.next();
    System.out.println(parentwindow);
    String childwindow = iterator.next();
    System.out.println(childwindow);
    driver.switchTo().window(childwindow);                    
                
            
    Iterator iterator = windowhandles.iterator();
  • Creates an iterator to iterate through the set of window handles.
  • String parentwindow = iterator.next();
  • Gets the first window handle (the original window).
  • String childwindow = iterator.next();
  • Gets the second window handle (the newly opened window).
  • driver.switchTo().window(childwindow);
  • Switches the WebDriver's context to the new window identified by childwindow.

5. Interacting with Elements in the New Window:

                
    Thread.sleep(4000);
    driver.findElement(By.name("UserFirstName")).sendKeys("Akhil");
    Thread.sleep(4000);
    driver.findElement(By.name("UserLastName")).sendKeys("Chukkalwar");
                
            
  • Interacts with elements in the new window, such as filling out a form with first and last names.

6. Switching Back to the Original Window:

                
    driver.switchTo().window(parentwindow);
                
            
  • Switches the WebDriver's context back to the original window. This allows for further interactions with the original page.

Key Points
  • Window Handles: Each window or tab opened by the WebDriver has a unique handle. These handles are used to switch between different windows or tabs.
  • Switching Context: Use
    driver.switchTo().window(windowHandle)
    to change the WebDriver's context to a specific window or tab.
  • Robustness: While Thread .sleep() is used here for simplicity, it's better to use WebDriver's explicit waits (e.g., WebDriver Wait) for more reliable automation, as they allow you to wait for specific conditions or elements to be available.