Sometimes, a web app opens new windows or tabs. You need to switch control to these windows to test them.
0
0
Switching between windows in Selenium Java
Introduction
When clicking a link opens a new browser tab and you want to check its content.
When a popup window appears and you need to interact with it.
When testing multi-window workflows like login or payment popups.
When verifying that the main window remains unchanged after opening a new window.
When closing a child window and returning to the main window.
Syntax
Selenium Java
String mainWindow = driver.getWindowHandle(); for (String windowHandle : driver.getWindowHandles()) { if (!windowHandle.equals(mainWindow)) { driver.switchTo().window(windowHandle); break; } }
getWindowHandle() returns the current window's ID.
getWindowHandles() returns all open window IDs.
Examples
This switches from the main window to the newly opened window.
Selenium Java
String mainWindow = driver.getWindowHandle(); // Switch to new window for (String window : driver.getWindowHandles()) { if (!window.equals(mainWindow)) { driver.switchTo().window(window); break; } }
Switch directly to a window if you know its ID.
Selenium Java
driver.switchTo().window("windowHandleId");Switch back to the main window using its handle after working in another window.
Selenium Java
driver.switchTo().window(mainWindow);
Sample Program
This test opens a page with a button that opens a new window. It switches to the new window, prints its title, closes it, then returns to the main window and prints its title.
Selenium Java
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import java.time.Duration; public class WindowSwitchTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5)); driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_open"); // Switch to iframe to click the button driver.switchTo().frame("iframeResult"); driver.findElement(By.tagName("button")).click(); String mainWindow = driver.getWindowHandle(); // Switch to new window for (String windowHandle : driver.getWindowHandles()) { if (!windowHandle.equals(mainWindow)) { driver.switchTo().window(windowHandle); break; } } // Print title of new window System.out.println("New window title: " + driver.getTitle()); // Close new window driver.close(); // Switch back to main window driver.switchTo().window(mainWindow); System.out.println("Back to main window title: " + driver.getTitle()); driver.quit(); } }
OutputSuccess
Important Notes
Always store the main window handle before switching to new windows.
Use driver.close() to close the current window, and driver.quit() to close all windows and end the session.
Switching windows is important to interact with popups or tabs opened by your test actions.
Summary
Switching windows lets your test control new browser tabs or popups.
Use getWindowHandle() and getWindowHandles() to manage windows.
Always switch back to the main window after finishing work in a new window.