Modern web applications often open links in new tabs. Sometimes, your test script needs to switch between them to complete a user journey or validate a feature.
Selenium lets you control tabs just like windows. You can open a new tab, switch focus, close it, and return to the original one. Once you know how to handle multiple tabs in Selenium, your test coverage improves dramatically.
In this guide, we’ll show you:
Let’s get started.
Selenium does not include a built-in function for opening new tabs, but you can still do it pretty easily by using key commands or JavaScript. Each approach works well in different test environments.
This is a helpful starting point when you're learning how to handle multiple tabs in Selenium. Here's how to open a new tab in three ways.
This method sends the control key and "t" to the browser window. It opens a blank new tab.
from selenium.webdriver.common.keys import Keys
driver.find_element("tag name", "body").send_keys(Keys.CONTROL + "t")
For macOS users, you’ll use the Command key instead. The result is the same—a fresh tab opens in the same browser window.
driver.find_element("tag name", "body").send_keys(Keys.COMMAND + "t")
JavaScript gives you direct control. You can open a tab and load a specific URL right away. This is the most reliable option when you want to test multiple flows across tabs.
driver.execute_script("window.open('https://katalon.com','_blank');")
All three methods help you start handling tabs in Selenium with confidence. If you're preparing for workflows that span tabs, you’re already one step ahead in learning how to handle multiple tabs in Selenium.
Once you know how to open a new tab, the next step is learning how to close it properly.
Selenium gives you two commands for this:
When handling multiple tabs in Selenium, always track which one you're on. Closing the wrong tab can break the test flow. You want to switch to the target tab, close it, then return to the original.
This script shows how to open two tabs, close the second one, and go back to the first:
driver.get("https://katalon.com")
original_tab = driver.current_window_handle
driver.execute_script("window.open('https://example.com', '_blank');")
tabs = driver.window_handles
driver.switch_to.window(tabs[1])
driver.close()
driver.switch_to.window(original_tab)
This flow works well when your test needs to confirm behaviors across tabs. It also avoids errors by making sure you’re always closing the right one. When learning how to handle multiple tabs in Selenium, managing tab focus is key.
Selenium handles browser tabs through a built-in property called window_handles. This gives you a list of all open tabs at any point during your test. You can use that list to switch tabs, perform actions, and move focus as needed.
This is the foundation of how to handle multiple tabs in Selenium. Once you get the tab handles, you can fully control the browser flow from one step to the next.
Let’s look at a complete example:
driver.get("https://katalon.com")
driver.execute_script("window.open('https://example.com','_blank');")
driver.execute_script("window.open('https://www.google.com','_blank');")
tabs = driver.window_handles
# Switch to second tab and print the title
driver.switch_to.window(tabs[1])
print(driver.title)
# Switch to third tab and print the title
driver.switch_to.window(tabs[2])
print(driver.title)
# Return to first tab
driver.switch_to.window(tabs[0])
This pattern gives you full control across tabs. You can switch focus, interact with elements, validate content, and more. The key is knowing when and where to apply the switch. If you're working with real user flows, this is an essential part of how to handle multiple tabs in Selenium.
Selenium uses window_handles to keep track of every open browser tab. It stores them in the order they were opened. You can use this list to loop through each tab, switch between them, and perform validations like checking the page title.
You also have access to the current tab with current_window_handle. This is useful when you want to return to your starting point after navigating across tabs.
This example opens a few tabs and prints the title of each one:
driver.get("https://katalon.com")
driver.execute_script("window.open('https://example.com','_blank');")
driver.execute_script("window.open('https://google.com','_blank');")
tabs = driver.window_handles
original_tab = driver.current_window_handle
for index, tab in enumerate(tabs):
driver.switch_to.window(tab)
print(f"Tab {index + 1} Title:", driver.title)
driver.switch_to.window(original_tab)
This loop gives you full visibility into each tab's content. It also keeps your test organized by returning focus to the original tab. When learning how to handle multiple tabs in Selenium, this kind of iteration helps manage flow and avoid confusion.
Selenium uses the same API to handle both browser windows and tabs. This unified approach makes it easy to switch between different views during a test, whether it’s a login page, a new feature walkthrough, or a third-party integration window.
To switch between tabs or windows, Selenium provides the window_handles list and the switch_to.window() method. You can store the handle of your current tab, move to the new one, and then switch back when needed. This helps ensure stability in workflows that depend on tab context.
Here’s a simple use case:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://katalon.com")
# Save the current window handle
main_tab = driver.current_window_handle
# Open a new tab
driver.execute_script("window.open('https://example.com', '_blank');")
time.sleep(2)
# Switch to the new tab
new_tab = driver.window_handles[1]
driver.switch_to.window(new_tab)
print("New tab title:", driver.title)
# Switch back to the main tab
driver.switch_to.window(main_tab)
print("Back to main tab title:", driver.title)
Working with browser tabs is smooth when you follow a few key checks. These common scenarios will help you avoid surprises and handle Selenium tabs with more control and clarity.
Sometimes Selenium opens a new tab but doesn’t detect it right away. You can use an explicit wait to pause until the number of tabs meets your expectation. This gives the new tab time to load.
If the tab is closed too soon, trying to switch to it may cause a NoSuchWindowException. You can avoid this by checking the handles list before switching.
Switching between tabs might change the context of your elements. If your test throws a stale element error, the best way is to re-locate the element once you’re in the correct tab.
Here are a few quick tips:
If you're learning how to handle multiple tabs in Selenium, it’s helpful to follow a few tested habits:
Katalon is a low-code automation platform built on top of Selenium. It makes testing more accessible with a modern interface and robust built-in features that remove the complexity of traditional test scripts.
With Katalon, you get:
In short, Katalon makes Selenium more productive. It offers the power of automation in a scalable, team-friendly package that grows with your projects.
📝 Want to explore what Katalon can do for your team? Request a demo to see how it helps teams automate faster with less effort.