Bird
Raised Fist0
Selenium Pythontesting~10 mins

Switching between windows in Selenium Python - Test Execution Trace

Choose your learning style10 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.

Practice

(1/5)
1. What does driver.switch_to.window(window_handle) do in Selenium?
easy
A. It closes the current browser window.
B. It changes control to the browser window identified by window_handle.
C. It refreshes the current browser window.
D. It opens a new browser window.

Solution

  1. Step 1: Understand the method purpose

    driver.switch_to.window() is used to change Selenium's focus to a different browser window or tab.
  2. Step 2: Identify the parameter role

    The parameter window_handle specifies which window Selenium should control next.
  3. Final Answer:

    It changes control to the browser window identified by window_handle. -> Option B
  4. Quick Check:

    Switching windows = change control [OK]
Hint: Switch windows by passing the window handle to switch_to.window() [OK]
Common Mistakes:
  • Thinking it closes or refreshes windows
  • Confusing switching with opening new windows
  • Not passing a valid window handle
2. Which of the following is the correct syntax to switch to a new window stored in variable new_window?
easy
A. driver.window_switch(new_window)
B. driver.switch_window(new_window)
C. driver.switchTo().window(new_window)
D. driver.switch_to.window(new_window)

Solution

  1. Step 1: Recall correct Selenium Python syntax

    The correct method to switch windows is driver.switch_to.window() in Python Selenium.
  2. Step 2: Match the syntax with the options

    driver.switch_to.window(new_window) uses the correct method and parameter. Other options use incorrect method names or Java syntax.
  3. Final Answer:

    driver.switch_to.window(new_window) -> Option D
  4. Quick Check:

    Python Selenium uses switch_to.window() [OK]
Hint: Use driver.switch_to.window(handle) exactly in Python [OK]
Common Mistakes:
  • Using Java syntax like switchTo()
  • Using incorrect method names
  • Missing the switch_to part
3. What will be the output of the following code snippet?
original = driver.current_window_handle
all_windows = driver.window_handles
for w in all_windows:
    if w != original:
        driver.switch_to.window(w)
        print(driver.title)
        break
print(driver.current_window_handle == original)
medium
A. Prints the title of the new window and then prints False
B. Prints the title of the original window and then prints True
C. Throws an error because of wrong window handle usage
D. Prints False twice

Solution

  1. Step 1: Understand window handles and switching

    The code saves the original window handle, loops through all windows, and switches to the first window that is not original.
  2. Step 2: Analyze the print statements

    Inside the loop, it prints the title of the new window and breaks. After the loop, it compares current window handle with original, which will be False because we switched.
  3. Final Answer:

    Prints the title of the new window and then prints False -> Option A
  4. Quick Check:

    Switched window title + current handle != original = False [OK]
Hint: Switch changes current handle, so comparison with original is False [OK]
Common Mistakes:
  • Assuming current window handle is still original after switch
  • Confusing window_handles list with current_window_handle
  • Expecting True after switching
4. Identify the error in this code snippet that tries to switch back to the original window:
original = driver.current_window_handle
# code opens new window
new_window = driver.window_handles[1]
driver.switch_to.window(new_window)
# some actions
# switch back
switch_to.window(original)
medium
A. Cannot switch back once switched
B. Using wrong index for new_window
C. Missing 'driver.' before switch_to.window in last line
D. No error, code is correct

Solution

  1. Step 1: Check the last line syntax

    The last line calls switch_to.window(original) without the driver. prefix, which causes a NameError.
  2. Step 2: Confirm other parts are correct

    Index 1 is valid if two windows exist, switching back is allowed, so no other errors.
  3. Final Answer:

    Missing 'driver.' before switch_to.window in last line -> Option C
  4. Quick Check:

    Always prefix with driver.switch_to.window() [OK]
Hint: Always use driver.switch_to.window() fully [OK]
Common Mistakes:
  • Forgetting 'driver.' before switch_to
  • Assuming switch back is automatic
  • Using wrong window handle index without checking
5. You have opened multiple tabs during a test. How do you switch back to the original tab after performing actions on others?
hard
A. Store the original window handle before switching, then use driver.switch_to.window(original_handle) to return.
B. Call driver.close() on all other tabs to return automatically.
C. Use driver.switch_to.default_window() to go back to the first tab.
D. Restart the browser to return to the original tab.

Solution

  1. Step 1: Save the original window handle

    Before switching tabs, save the current window handle in a variable.
  2. Step 2: Switch back using saved handle

    Use driver.switch_to.window(original_handle) to return control to the original tab.
  3. Step 3: Evaluate other options

    Closing tabs or restarting browser is inefficient; default_window() does not exist in Selenium Python.
  4. Final Answer:

    Store the original window handle before switching, then use driver.switch_to.window(original_handle) to return. -> Option A
  5. Quick Check:

    Save handle, then switch back using it [OK]
Hint: Always save original handle to switch back later [OK]
Common Mistakes:
  • Trying to use non-existent default_window() method
  • Closing tabs instead of switching
  • Restarting browser unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard