0
0
Selenium Pythontesting~10 mins

Window handles in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a new browser window, switches control to it, 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 TestWindowHandles(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com')

    def test_window_handles(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
        windows = driver.window_handles
        for window in windows:
            if window != original_window:
                driver.switch_to.window(window)
                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.assertIn('Example Domain', driver.title)

    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
2Stores current window handle as original_windowOne browser window open with handle stored-PASS
3Waits for and clicks link with text 'More information...' that opens new windowUser clicks link, new window opening-PASS
4Waits until new window is openedTwo browser windows open-PASS
5Switches control to the new windowDriver controls new window-PASS
6Checks that new window title contains 'IANA'New window title visibleAssert 'IANA' in driver.titlePASS
7Switches back to original windowDriver controls original window-PASS
8Checks that original window title contains 'Example Domain'Original window title visibleAssert 'Example Domain' in driver.titlePASS
9Test ends and browser closesNo browser windows open-PASS
Failure Scenario
Failing Condition: The link with text 'More information...' is not found or clickable
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do after clicking the link that opens a new window?
AWaits for the new window to open and switches to it
BCloses the original window immediately
CRefreshes the current page
DClicks the link again
Key Result
Always store the original window handle before opening new windows, so you can switch back reliably after testing the new window.