0
0
Selenium Pythontesting~15 mins

Why multi-window scenarios need switching in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Verify multi-window handling by switching between windows
Preconditions (2)
Step 1: Open the main web page with a link that opens a new window
Step 2: Click the link that opens a new window
Step 3: Switch to the new window
Step 4: Verify the new window's title or URL
Step 5: Switch back to the original window
Step 6: Verify the original window's title or URL
✅ Expected Result: The test should successfully switch between windows and verify their titles or URLs correctly.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the new window's title or URL after switching
Verify the original window's title or URL after switching back
Best Practices:
Use explicit waits to ensure windows are loaded before switching
Use window handles to switch windows instead of hardcoded indices
Clean up by closing extra windows if needed
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Initialize WebDriver
with webdriver.Chrome() as driver:
    driver.get('https://example.com/multi-window')  # Replace with actual URL

    original_window = driver.current_window_handle

    # Click link that opens new window
    link = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, 'open-new-window'))
    )
    link.click()

    # Wait for new window
    WebDriverWait(driver, 10).until(EC.new_window_is_opened)

    # Get all window handles
    windows = driver.window_handles

    # Switch to new window
    for window in windows:
        if window != original_window:
            driver.switch_to.window(window)
            break

    # Verify new window title or URL
    WebDriverWait(driver, 10).until(EC.title_contains('New Window'))
    assert 'new-window' in driver.current_url, f"Expected 'new-window' in URL but got {driver.current_url}"

    # Switch back to original window
    driver.switch_to.window(original_window)

    # Verify original window title or URL
    WebDriverWait(driver, 10).until(EC.title_contains('Main Page'))
    assert 'multi-window' in driver.current_url, f"Expected 'multi-window' in URL but got {driver.current_url}"

This script opens a web page that has a link opening a new window. It stores the original window handle first. Then it clicks the link and waits until a new window opens.

It collects all window handles and switches to the one that is not the original. Then it waits for the new window's title to contain expected text and asserts the URL contains expected substring.

After verification, it switches back to the original window and verifies its title and URL similarly.

Explicit waits ensure the windows are fully loaded before assertions. Using window handles avoids hardcoding window indices, making the test reliable.

Common Mistakes - 3 Pitfalls
Not switching to the new window before interacting
Hardcoding window indices like driver.window_handles[1]
Not waiting for the new window to open before switching
Bonus Challenge

Now add data-driven testing with 3 different links that open different new windows

Show Hint