0
0
Selenium Pythontesting~15 mins

Switching between windows in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify switching between browser windows
Preconditions (2)
Step 1: Open the main page URL
Step 2: Click the link or button that opens a new window
Step 3: Switch to the new window
Step 4: Verify the new window's title is as expected
Step 5: Close the new window
Step 6: Switch back to the original window
Step 7: Verify the original window's title is as expected
✅ Expected Result: The test switches to the new window correctly, verifies its title, closes it, and returns to the original window verifying its title.
Automation Requirements - Selenium with Python
Assertions Needed:
Assert the new window's title matches expected
Assert the original window's title matches expected after switching back
Best Practices:
Use explicit waits to ensure elements are ready before interaction
Store window handles properly to switch between windows
Close only the new window before switching back
Use try-finally or context management to clean up windows
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 the WebDriver (Chrome example)
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

try:
    # Step 1: Open the main page
    driver.get('https://example.com/mainpage')
    original_window = driver.current_window_handle

    # Step 2: Click the link/button that opens a new window
    open_window_button = wait.until(EC.element_to_be_clickable((By.ID, 'open-new-window')))
    open_window_button.click()

    # Step 3: Wait for the new window and switch to it
    wait.until(EC.number_of_windows_to_be(2))
    new_window = [window for window in driver.window_handles if window != original_window][0]
    driver.switch_to.window(new_window)

    # Step 4: Verify the new window's title
    wait.until(EC.title_is('New Window Title'))
    assert driver.title == 'New Window Title', f"Expected title 'New Window Title', got '{driver.title}'"

    # Step 5: Close the new window
    driver.close()

    # Step 6: Switch back to the original window
    driver.switch_to.window(original_window)

    # Step 7: Verify the original window's title
    wait.until(EC.title_is('Main Page Title'))
    assert driver.title == 'Main Page Title', f"Expected title 'Main Page Title', got '{driver.title}'"

finally:
    driver.quit()

This script uses Selenium WebDriver with Python to automate switching between browser windows.

First, it opens the main page and stores the original window handle.

Then it clicks a button that opens a new window and waits until two windows are present.

It finds the new window handle by excluding the original one and switches to it.

It waits for the new window's title to be correct and asserts it matches the expected title.

Next, it closes the new window and switches back to the original window.

Finally, it waits for the original window's title and asserts it is correct.

The try-finally block ensures the browser closes even if assertions fail.

Common Mistakes - 4 Pitfalls
Not waiting for the new window to open before switching
Closing the original window instead of the new window
Using hardcoded window handles
Not switching back to the original window after closing the new one
Bonus Challenge

Now add data-driven testing with 3 different buttons that open different new windows with distinct titles.

Show Hint