Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

How to make another window in Selenium?

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.

What Is a Window Handle in Selenium?

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.

Opening a New Window in Selenium 4

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.

Switching to a Window Opened by a Link

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);

Creating a New Window in Python

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.

Common Mistakes and Troubleshooting

  • Not saving the parent handle: Without storing getWindowHandle() first, you lose the reference and cannot switch back.
  • Acting before switching: Opening a window with a click does not shift focus automatically (unlike newWindow). You must call switchTo().window() first.
  • NoSuchWindowException: Switching to a handle that was already closed throws this error. Refresh the handle set after closing a window.
  • Race conditions: The new window may not appear instantly. Use an explicit wait on the expected handle count instead of a fixed sleep.
  • Using deprecated methods: switch_to_window in Python is deprecated; use switch_to.window to avoid warnings and future breakage.

Running Multi-Window Tests Across Real Browsers

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#.

Conclusion

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.

Frequently Asked Questions

How do you open a new window in Selenium 4?

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.

What is the difference between getWindowHandle and getWindowHandles?

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.

How do you switch back to the parent window in Selenium?

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.

What is the difference between a window and a tab in Selenium?

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.

How do you count open windows in Selenium?

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.

Why does Selenium throw NoSuchWindowException?

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.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

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

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests