Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

In Selenium 4 you can create another window directly with driver.switchTo().newWindow(WindowType.WINDOW), which opens a new browser window and moves focus to it automatically. In earlier versions, or when a window opens after a link click, you capture handles with getWindowHandles() and move focus using switchTo().window(). Every window is tracked by a unique window handle.
When Selenium opens a browser window or tab, it assigns that window a unique alphanumeric identifier called a window handle. Because each handle is unique, Selenium uses it to distinguish and switch between windows. getWindowHandle() returns the handle of the current window, while getWindowHandles() returns a set of every open window's handle.
Selenium 4 introduced a dedicated command that removes the old JavaScript workarounds. It creates the window and switches focus in a single call.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
public class NewWindowDemo {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.example.com");
// Store the parent window handle
String parent = driver.getWindowHandle();
// Open a brand new window and switch to it
driver.switchTo().newWindow(WindowType.WINDOW);
driver.get("https://www.selenium.dev");
// Do work, then return to the parent
driver.close();
driver.switchTo().window(parent);
driver.quit();
}
}Swap WindowType.WINDOW for WindowType.TAB if you want a new tab instead of a separate window.
When a click opens a new window, you cannot control it directly. Save the parent handle first, then iterate the updated handle set to find and switch to the new one.
String parent = driver.getWindowHandle();
driver.findElement(By.linkText("Open new window")).click();
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(parent)) {
driver.switchTo().window(handle);
break;
}
}
// Work in the new window, then switch back
driver.close();
driver.switchTo().window(parent);The Python bindings expose the same capabilities. Note that switch_to_window is deprecated in favor of switch_to.window.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.example.com")
parent = driver.current_window_handle
# Selenium 4 native new window
driver.switch_to.new_window('window')
driver.get("https://www.selenium.dev")
print("Open windows:", len(driver.window_handles))
driver.close()
driver.switch_to.window(parent)
driver.quit()For a fuller walkthrough, the switch tabs in a browser using Selenium Python guide expands on multi-window flows.
Multi-window behavior can differ subtly between browsers and versions, so validating on a single local browser is risky. TestMu AI lets you run Selenium tests across a cloud of 3000+ real browsers and operating systems, so window handling, pop-ups, and tab switching behave consistently for every user. You can move existing scripts to the cloud with a few capability changes and scale them in parallel using Selenium automation and cross browser testing. The handling multiple browser windows tutorial covers the same patterns in C#.
Making another window in Selenium is straightforward once you understand window handles. In Selenium 4, reach for newWindow(WindowType.WINDOW) for programmatic control; for windows opened by user actions, store the parent handle, iterate getWindowHandles(), and switch focus deliberately. Always return to the parent handle when you are done to keep the rest of your test stable.
Selenium 4 adds a native command: driver.switchTo().newWindow(WindowType.WINDOW) opens a new window and automatically switches focus to it. Use WindowType.TAB instead if you want a new tab rather than a separate window.
getWindowHandle() returns a single string for the currently focused window, typically used to remember the parent. getWindowHandles() returns a Set of strings for every open window, which you iterate to find and switch to a new one.
Store the parent handle with getWindowHandle() before opening the new window. After finishing work in the child window and closing it, call driver.switchTo().window(parentHandle) to return focus to the original window.
Functionally Selenium treats both the same, using window handles to identify them. The difference is only how they open: WindowType.WINDOW creates a separate browser window while WindowType.TAB opens a tab within the current window.
Get the collection of handles with getWindowHandles() and read its size, for example driver.getWindowHandles().size() in Java or len(driver.window_handles) in Python, to know how many windows or tabs are currently open.
It occurs when you try to switch to or act on a window handle that has already been closed or never existed. Always refresh the handle set after opening or closing windows and switch back to a valid handle before continuing.
KaneAI - Testing Assistant
World’s first AI-Native E2E testing agent.

TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance