Modern web apps often open a new browser tab for login flows, payment pages, documentation links, or social sharing. When that happens, your test has to move with the browser. If Selenium keeps running in the original tab, it will miss what the user actually sees in the new one.
In this guide, you'll learn how to switch tabs in Selenium with Python using the most reliable patterns for modern test automation. This is sometimes called switching windows, or simply handling tabs in Selenium, but the underlying mechanics are the same. We’ll cover:
THE SHORT ANSWER
To switch tabs in Selenium with Python, get the current tab handle with driver.current_window_handle, open or detect the new tab, then switch to it with driver.switch_to.window(...). In Selenium 4, the cleanest way to open a new tab is driver.switch_to.new_window('tab'). For stability, wait until the new tab exists before switching and re-locate elements after changing tabs.
In Selenium, tabs and windows are handled through the same API. Each browser tab gets a unique window handle, and Selenium uses that handle to move control from one tab to another.
The two most important pieces are:
This means tab handling and window handling are conceptually the same in Selenium. The only difference is what the browser chooses to open.
If you're using Selenium 4, the best option is the native API:
driver.switch_to.new_window('tab')
driver.get('https://example.com')
This is more reliable than keyboard shortcuts and clearer than JavaScript-based workarounds. If you need to support older patterns, you can still use:
driver.execute_script("window.open('https://example.com', '_blank')")
Keyboard shortcuts such as Ctrl+T or Command+T can work in some cases, but they are more browser-dependent and less reliable in automated test runs.
Here is a stable pattern that stores the original tab, opens a new one, waits for it, and switches safely:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.get("https://katalon.com")
original_tab = driver.current_window_handle
existing_tabs = driver.window_handles.copy()
driver.switch_to.new_window('tab')
driver.get("https://example.com")
WebDriverWait(driver, 10).until(lambda d: len(d.window_handles) == len(existing_tabs) + 1)
new_tab = [handle for handle in driver.window_handles if handle not in existing_tabs][0]
driver.switch_to.window(new_tab)
print("Now on:", driver.title)
This approach is safer than assuming the second tab is always at index 1, especially in longer or more dynamic sessions.
Many test flows open a new tab as part of the normal user journey. Common examples include:
If you know the tab order and your flow is simple, you can switch by list position:
tabs = driver.window_handles
if len(tabs) > 1:
driver.switch_to.window(tabs[1])
print("Switched to tab at index 1")
This works, but it is usually better to switch by comparing handles rather than assuming a fixed order.
Once you finish working in the secondary tab, switch back using the original handle you stored earlier:
driver.switch_to.window(original_tab)
print("Back on main tab")
Saving the original handle at the start of the flow is one of the simplest ways to make tab automation more reliable.
Use the right command for the job:
Here's the safe pattern:
driver.get("https://katalon.com")
original_tab = driver.current_window_handle
driver.switch_to.new_window('tab')
driver.get("https://example.com")
driver.close()
driver.switch_to.window(original_tab)
To inspect what is open and which tab is active, use:
main_tab = driver.current_window_handle
all_tabs = driver.window_handles
for handle in all_tabs:
driver.switch_to.window(handle)
print("Tab title:", driver.title)
This is especially useful for debugging flows that open multiple tabs unexpectedly.
The same pattern scales to three or more tabs. Loop through the handles, inspect each tab, and return to the original when finished:
driver.get("https://katalon.com")
original_tab = driver.current_window_handle
driver.switch_to.new_window('tab')
driver.get("https://example.com")
driver.switch_to.new_window('tab')
driver.get("https://google.com")
for index, handle in enumerate(driver.window_handles, start=1):
driver.switch_to.window(handle)
print(f"Tab {index} title:", driver.title)
driver.switch_to.window(original_tab)
| Symptom | Likely cause | Fix |
|---|---|---|
| New tab is not detected | The script switches too quickly | Wait for the expected number of window handles before switching |
| NoSuchWindowException | The tab was already closed or the handle is invalid | Confirm the handle still exists in driver.window_handles |
| Stale element errors after switching | Elements were located in a different tab context | Re-locate the element after switching to the active tab |
Katalon is an automation platform built on Selenium that helps teams handle browser workflows with less manual overhead. Instead of wiring tab logic together from scratch every time, teams can use built-in browser keywords, execution infrastructure, reporting, and maintenance tools in one place.
With Katalon, teams get:
To learn more, visit the Katalon Docs or explore guided learning paths in Katalon Academy.
Want to explore what Katalon can do for your team? Request a demo to see how it helps teams automate faster with less effort.