Browser Tabs

Introduction

Modern web applications frequently open links in a new browser tab instead of the current page. Examples include product details, documentation, reports, help pages, social media links, and third-party websites.

From Selenium’s perspective, browser tabs and browser windows are handled exactly the same way. Each open tab receives a unique Window Handle, allowing Selenium to identify and switch between tabs.

In this tutorial, you’ll learn how to automate browser tabs using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.


What are Browser Tabs?

A Browser Tab is another page opened within the same browser window.

For example:

Chrome

---------------------------------------
| Main Page | Documentation | Payment |
---------------------------------------

Although they appear as tabs in the browser, Selenium treats each tab as a separate browser context with its own unique window handle.


How Does Selenium Handle Browser Tabs?

Selenium uses:

  • driver.window_handles

  • driver.switch_to.window()

to identify and switch between browser tabs.

Each newly opened tab receives a unique window handle.


Why Automate Browser Tabs?

Automating browser tabs helps you:

  • Test external links.

  • Verify documentation pages.

  • Automate authentication flows.

  • Test payment gateways.

  • Improve automation coverage.


Example

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait


# Topic: 30. Windows and Tabs - Browser Tabs
# Practice site: https://the-internet.herokuapp.com/windows
# Run: pytest -s 30_examples/test_02_browser_tabs.py
#
# Most browsers open target="_blank" links in a new tab. Selenium treats tabs
# and windows the same way: both are handled by window handles.


def test_new_tab_opens_from_link():
    driver = webdriver.Chrome()

    try:
        driver.get("https://the-internet.herokuapp.com/windows")

        driver.find_element(By.LINK_TEXT, "Click Here").click()

        WebDriverWait(driver, 10).until(
            lambda browser: len(browser.window_handles) == 2
        )

        driver.switch_to.window(driver.window_handles[-1])

        assert driver.find_element(By.TAG_NAME, "h3").text == "New Window"
    finally:
        driver.quit()

Understanding the Code

Import Required Libraries

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

These modules are required to launch the browser, locate web elements, and wait for the new browser tab to open.


Create a Chrome Browser Instance

driver = webdriver.Chrome()

Starts a new Chrome browser session.


Open the Practice Website

driver.get("https://the-internet.herokuapp.com/windows")

Navigates to the practice page.


Open a New Browser Tab

driver.find_element(
    By.LINK_TEXT,
    "Click Here"
).click()

Clicks the Click Here link.

Most modern browsers open this link in a new browser tab or window.


Wait Until Two Browser Contexts Exist

WebDriverWait(driver, 10).until(
    lambda browser: len(browser.window_handles) == 2
)

Waits until Selenium detects two window handles.

This ensures the new tab has opened successfully.


Switch to the New Tab

driver.switch_to.window(driver.window_handles[-1])

Switches Selenium’s control to the last opened browser tab.


Verify the New Page

assert driver.find_element(
    By.TAG_NAME,
    "h3"
).text == "New Window"

Retrieves the page heading and verifies that the new browser tab has opened correctly.


Close the Browser

driver.quit()

Closes all browser tabs and ends the WebDriver session.


Practical Example

Suppose an online shopping website opens the product manual in a new browser tab.

The automation script:

  • Opens the product page.

  • Clicks User Manual.

  • Switches to the new tab.

  • Verifies that the manual page is displayed.


Automation Testing Example

Consider an HR portal.

Clicking Company Policies opens a PDF in a new browser tab.

The automation script:

  • Opens the HR portal.

  • Clicks the policy document.

  • Switches to the new tab.

  • Verifies that the correct document is displayed.


Real-World Example

Browser tabs are commonly used in:

  • Banking applications

  • E-commerce websites

  • Government portals

  • HR systems

  • Documentation websites

  • Payment gateways

  • Enterprise web applications

Examples include opening documentation, invoices, reports, help pages, and external websites.


Advantages of Automating Browser Tabs

  • Supports real user workflows.

  • Verifies external navigation.

  • Tests documentation pages.

  • Improves automation coverage.

  • Supports multi-page testing.


Common Mistakes Beginners Make

Assuming Tabs Are Different from Windows

Selenium treats browser tabs and browser windows exactly the same.

Both use window handles.


Switching Before the Tab Opens

Always wait until the expected number of window handles is available.


Assuming the New Tab Is Always at Index 1

Instead of hardcoding an index, retrieve the latest window handle dynamically whenever possible.


Forgetting to Switch to the New Tab

Attempting to locate elements in the new tab without switching first usually results in a NoSuchElementException.


Best Practices

  • Wait until the new tab opens.

  • Switch using driver.switch_to.window().

  • Verify the page after switching.

  • Use driver.window_handles to manage browser contexts.

  • Close browser sessions properly after testing.


Conclusion

Browser tabs are widely used in modern web applications for opening external pages, documentation, reports, and payment workflows. Selenium manages browser tabs using the same window handle mechanism as browser windows, making it easy to switch between them. Understanding browser tab handling is essential for creating reliable end-to-end Selenium automation scripts.


Frequently Asked Questions (FAQs)

Does Selenium treat browser tabs and browser windows differently?

No.

Selenium treats browser tabs and browser windows the same by assigning each one a unique window handle.


How do I switch to another browser tab?

Use:

driver.switch_to.window(window_handle)

How do I retrieve all open tabs?

Use:

driver.window_handles

Why should I use an Explicit Wait before switching tabs?

Because the new tab may take a moment to open. Waiting ensures Selenium can detect the new browser context before switching.


Where are browser tabs commonly used?

Browser tabs are commonly used for documentation, reports, invoices, payment pages, external links, help pages, and authentication workflows.


Key Takeaways

  • Selenium treats browser tabs and windows the same way.

  • Every browser tab receives a unique window handle.

  • Use driver.window_handles to retrieve all open tabs.

  • Use driver.switch_to.window() to switch between tabs.

  • Wait until the new tab opens before switching.

  • Browser tab handling is an essential Selenium automation skill.