0
0
Selenium Pythontesting~10 mins

Switching between windows in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage, clicks a link that opens a new browser window, switches to the new window, verifies the new window's title, then switches back to the original window and verifies its title.

Test Code - Selenium
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
import unittest

class TestWindowSwitching(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com')

    def test_switch_windows(self):
        driver = self.driver
        original_window = driver.current_window_handle

        # Click link that opens a new window
        link = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.LINK_TEXT, 'More information...'))
        )
        link.click()

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

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

        # Verify new window title
        self.assertIn('IANA', driver.title)

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

        # Verify original window title
        self.assertEqual(driver.title, 'Example Domain')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and opens browser to 'https://example.com'Browser shows 'Example Domain' page-PASS
2Waits until link with text 'More information...' is clickableLink is visible and clickable on the page-PASS
3Clicks the 'More information...' linkNew browser window opens with IANA page-PASS
4Waits until a new window is openedTwo browser windows are open-PASS
5Switches control to the new windowDriver controls the new window showing IANA page-PASS
6Checks that the new window's title contains 'IANA'New window title is 'IANA — IANA-managed Reserved Domains'Title contains 'IANA'PASS
7Switches back to the original windowDriver controls original window showing 'Example Domain'-PASS
8Verifies the original window's title is exactly 'Example Domain'Original window title is 'Example Domain'Title equals 'Example Domain'PASS
9Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: The link with text 'More information...' is not found or clickable, or new window does not open
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do immediately after clicking the link?
AWaits for a new window to open
BCloses the browser
CChecks the original window title
DSwitches back to the original window
Key Result
Always store the original window handle before opening a new window so you can switch back reliably.