Â
Handling multiple windows in Selenium WebDriver involves switching between different windows or tabs that are open in the browser.Â
The main steps to handle multiple windows in Selenium WebDriver with Java:
- Storing the handle of the main window: Before opening any new windows, it's important that we store the handle of the main window so that we can switch back to it later. so for that purpose we need to use getWindowHandle()Â in our script.
- Opening a new window: This can be done by clicking a link or button that opens a new window driver.findElement(By.xpath()).click;
- Storing the handles of all open windows: For storing the handles of multiple windows Selenium provides the getWindowHandles() method to get the handles of all open windows in a set.
- Iterating through the set of windows: Using a loop, we can iterate through the set of windows and switch to each one using the switchTo().window() method.
- Performing actions on the new window: Once we have switched to the new window, we can perform various actions on it such as interacting with elements or taking screenshots.
- Closing the new window: After we have finished interacting with the new window, we can close it using the close() method.
- Switching back to the main window: Finally, we can switch back to the main window using the handle stored earlier and close it if necessary.
Below Code for Multiple Window Handling:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.Set; public class MultiWindowHandling { public static void main(String[] args) { // Set up WebDriver and open the main page System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); // Replace with the actual URL driver.manage().window().maximize(); // Locate and click on a link that opens a new window WebElement link = driver.findElement(By.id("openNewWindow")); link.click(); // Get the handle of the main window String mainWindowHandle = driver.getWindowHandle(); System.out.println("Main Window Handle: " + mainWindowHandle); // Get all window handles Set < String > allWindowHandles = driver.getWindowHandles(); System.out.println("All Window Handles: " + allWindowHandles); // Switch to the newly opened window for (String handle: allWindowHandles) { if (!handle.equals(mainWindowHandle)) { driver.switchTo().window(handle); System.out.println("Switched to new window: " + handle); // Perform actions in the new window System.out.println("New Window Title: " + driver.getTitle()); driver.close(); // Close the new window } } // Switch back to the main window driver.switchTo().window(mainWindowHandle); System.out.println("Switched back to main window: " + driver.getTitle()); // Close the main window driver.quit(); } }
Types of Switching Between Windows:
- Use getWindowHandle() for the current window.
- Use getWindowHandles() to get all window handles and switch using switchTo().window(handle).
- Iterate through all window handles and switch to the window where driver.getTitle() matches a specific title.
for (String handle: driver.getWindowHandles()) {driver.switchTo().window(handle);if (driver.getTitle().equals("Expected Title")) {break;}}
Â
3. By Web Element (Indirect):
- Clicking a web element that opens a new window, then switching to the new handle.
 4. By Index (Custom Logic):
- Store all window handles in a list and switch to a specific index.
Post a Comment