Many web applications open links, payment gateways, or login workflows in a new browser tab. If automation scripts cannot handle multiple tabs, critical test scenarios are left incomplete. Selenium Python provides several methods to open, switch, and manage tabs during automated testing, ensuring that multi-tab workflows are thoroughly validated.
Selenium treats both tabs and windows in the same way. Each tab is represented by a unique window handle—an identifier that allows Selenium to differentiate between open contexts.
By storing and switching between these handles, testers can control multiple browser tabs within a single session.
Before working with tabs, the environment must be set up correctly:
Install Selenium via pip:
pip install selenium
Import necessary modules:
from selenium import webdriver
from selenium.webdriver.common.by import By
With this setup, the driver is ready to open and manage multiple tabs.
Selenium 4 introduced switch_to.new_window('tab') for opening tabs natively.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
# Open a new tab
driver.switch_to.new_window('tab')
driver.get("https://www.python.org")
In earlier Selenium versions, JavaScript execution was often used:
driver.execute_script("window.open('https://www.python.org','_blank');")
Both approaches allow new tabs to be added to the session.
Each tab has a unique handle stored in driver.window_handles.
tabs = driver.window_handles
print(tabs) # List of open tab handles
Switching to a tab requires passing its handle:
driver.switch_to.window(tabs[1]) # Switch to the second tab
This method ensures Selenium interacts with the correct tab before performing actions.
When more than two tabs are open, relying on hardcoded indices is risky. Instead, a loop can identify the correct tab by checking its title or URL.
for handle in driver.window_handles:
driver.switch_to.window(handle)
if "Python" in driver.title:
break
This approach ensures the script always selects the right tab.
Best practice is to store the original tab’s handle before switching.
main_tab = driver.current_window_handle
# Perform actions in a new tab
driver.switch_to.new_window('tab')
driver.get("https://www.selenium.dev")
driver.close()
# Return to original tab
driver.switch_to.window(main_tab)
This prevents the script from losing track of the initial context.
Closing unnecessary tabs keeps the test session organized.
driver.close() # Closes current tab
driver.quit() # Closes entire browser session
When multiple tabs are open, explicitly switching before closing avoids unintentional termination of the main window.
Here are some of the common challenges when handling tabs in Selenium Python:
Always store the primary tab handle before opening new ones to ensure a safe return point.
Avoid premature interactions by applying waits:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 10).until(EC.title_contains("Python"))
Instead of assuming the new tab is always tabs[1], validate using title, URL, or page elements.
Tab handling can behave differently across operating systems and browsers. For instance, Safari on macOS may treat popups differently from Chrome on Windows. Mobile browsers often open new tabs as separate windows entirely.
To ensure test reliability, running Selenium Python scripts on real browsers and devices is essential. With testing tools like, BrowserStack Automate, testers can execute tab-switching tests across a wide range of environments in the cloud, without maintaining local infrastructure. This guarantees accurate results for workflows like payment redirects, OAuth logins, and document previews.
Switch based on URL:
for handle in driver.window_handles:
driver.switch_to.window(handle)
if "python.org" in driver.current_url:
break
Wait for specific content before interacting:
new_tab = driver.window_handles[-1]
driver.switch_to.window(new_tab)
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "downloads"))
)
These approaches make tab handling robust and reliable across different scenarios.
Switching tabs in Selenium Python is essential for testing workflows that span multiple browser contexts. By mastering window handles, using switch_to.window(), and applying best practices like explicit waits and real device validation, testers can avoid common pitfalls.
Incorporating BrowserStack Automate further strengthens testing by ensuring that tab management scripts work consistently across real devices, operating systems, and browsers.
Run Selenium Tests on Cloud
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop