0
0
Selenium Pythontesting~10 mins

Why multi-window scenarios need switching in Selenium Python - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test opens a main web page, clicks a link that opens a new browser window, switches to the new window, verifies its title, then switches back to the original window and verifies its title again. It shows why switching between windows is necessary to interact with elements in each window.

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 TestMultiWindowSwitch(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com')

    def test_window_switching(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
        new_window = [w for w in driver.window_handles if w != original_window][0]
        driver.switch_to.window(new_window)

        # 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 - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and opens browser to https://example.comBrowser shows the Example Domain page-PASS
2Finds the link with text 'More information...' and clicks itA new browser window opens with IANA page-PASS
3Waits until a new window is openedTwo browser windows are open: original and new-PASS
4Switches control to the new windowDriver context is now the new window showing IANA pageCheck that new window title contains 'IANA'PASS
5Switches control back to the original windowDriver context is back to original window showing Example DomainCheck that original window title contains 'Example Domain'PASS
6Test ends and browser closesBrowser windows closed-PASS
Failure Scenario
Failing Condition: Test fails if the driver does not switch to the new window before checking its title
Execution Trace Quiz - 3 Questions
Test your understanding
Why do we need to switch to the new window in this test?
ABecause Selenium can only interact with the current window context
BBecause the new window has a different URL
CBecause the new window loads faster
DBecause the original window closes automatically
Key Result
Always switch the driver context to the correct window handle before interacting with elements or verifying content in multi-window scenarios.