How To Handle Tabs in Selenium? A Complete Guide
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:
- How to open and close browser tabs during test runs
- How to switch between tabs and manage tab handles
- Common issues when working with multiple tabs
- Best practices to handle multiple tabs in Selenium without confusion
Let’s get started.
How to Open a New Tab in Selenium?
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.
1. Open a new tab with keyboard shortcut (Windows or Linux)
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")
2. Open a new tab with keyboard shortcut (Mac)
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")
3. Open a new tab with JavaScript
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.
How to close tab 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:
- Use driver.close() to shut the current tab
- Use driver.quit() to end the entire session.
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.
How to handle multiple tabs in Selenium?
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.
Handling multiple tabs using Window Handler
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.
Switching windows or tabs in Selenium
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:
- Store the current tab as the main app tab.
- Open a new tab for user login or third-party interaction.
- Switch back to the main tab after the process completes.
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)
Common issues when handling tabs in Selenium
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:
- Always wait for the new tab with WebDriverWait and number_of_windows_to_be().
- Check if the handle exists in window_handles before switching.
- Use driver.find_element() again after switching to ensure your element is fresh.
- Store the original tab handle at the beginning so you can return to it easily.
Best practices for handling tabs in Selenium
If you're learning how to handle multiple tabs in Selenium, it’s helpful to follow a few tested habits:
- Always save the original tab handle before launching new ones. This lets you come back to your starting point at any time with full control.
- New tabs might not be ready right away. Use explicit waits to make sure the page fully loads before you interact with it.
- Wrap your tab-switching logic into helper functions. This keeps your code short, clear, and easy to reuse across test cases.
- Closing tabs you no longer use keeps the session lightweight and focused. It helps reduce memory usage and prevents confusion when looping through multiple handles.
- Handling multiple tabs in Selenium becomes much simpler when your code is clean and your waits are well placed. Each step builds confidence into your automation flow.
Why choose Katalon to automate tests?
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:
- A complete environment for designing tests, executing them across environments, managing results, and generating reports. You can focus on building quality tests rather than stitching together tools.
- Katalon supports cross-browser and cross-platform execution out of the box. You can run your tests on thousands of browser and OS combinations without setting up infrastructure or drivers manually.
- Katalon integrates easily with CI/CD pipelines and cloud environments. You can run tests in parallel and shorten feedback loops with minimal setup.
- With AI-powered self-healing locators, Katalon adapts when your application UI changes, making your tests more resilient.
- Dashboards and detailed reports let you track coverage, performance, and quality over time.
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.
