Bird
Raised Fist0
Selenium Pythontesting~20 mins

Window handles in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Window Handles Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Python code snippet?
Consider the following Selenium Python code that opens a new tab and switches to it. What will be the value of current_handle after execution?
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

browser = webdriver.Chrome()
browser.get('https://example.com')
original_handle = browser.current_window_handle
browser.execute_script("window.open('https://example.org', '_blank');")
time.sleep(1)
handles = browser.window_handles
for handle in handles:
    if handle != original_handle:
        browser.switch_to.window(handle)
        break
current_handle = browser.current_window_handle
print(current_handle == original_handle)
browser.quit()
AFalse
BRaises NoSuchWindowException
CTrue
DNone
Attempts:
2 left
💡 Hint
Think about which window handle is active after switching.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies switching back to the original window?
You have stored the original window handle in original_handle. After opening and switching to a new window, which assertion correctly confirms you switched back to the original window?
Aassert browser.current_window_handle == original_handle
Bassert browser.current_window_handle != original_handle
Cassert browser.window_handles[0] == original_handle
Dassert browser.current_url == original_handle
Attempts:
2 left
💡 Hint
Check the current window handle after switching back.
❓ locator
advanced
1:30remaining
Which locator strategy is best to find a link that opens a new window?
You want to find a link that opens a new window when clicked. The link has the text 'Open New Window'. Which locator is best to find this link in Selenium?
Abrowser.find_element(By.ID, 'open-new-window')
Bbrowser.find_element(By.CLASS_NAME, 'new-window-link')
Cbrowser.find_element(By.LINK_TEXT, 'Open New Window')
Dbrowser.find_element(By.CSS_SELECTOR, 'a[target="_blank"]')
Attempts:
2 left
💡 Hint
The link text is unique and exact.
🔧 Debug
advanced
2:00remaining
Why does this Selenium code raise an error when switching windows?
This code snippet raises a NoSuchWindowException. What is the cause?
Selenium Python
browser = webdriver.Chrome()
browser.get('https://example.com')
original_handle = browser.current_window_handle
browser.switch_to.window('non_existent_handle')
AThe browser has not loaded the page yet
BThe handle 'non_existent_handle' does not exist in browser.window_handles
CThe driver is not initialized properly
DThe switch_to method is deprecated
Attempts:
2 left
💡 Hint
Check if the handle you switch to is valid.
❓ framework
expert
2:30remaining
In a test framework, how to ensure cleanup of all windows after test?
You write tests that open multiple windows. Which approach best ensures all windows are closed after each test to avoid side effects?
ADo nothing; windows close automatically after test ends
BOnly call browser.quit() at the end of all tests
CClose only the current window in teardown, leave others open
DIn a teardown method, iterate over browser.window_handles and close each, then quit the browser
Attempts:
2 left
💡 Hint
Think about cleaning all opened windows to avoid test interference.

Practice

(1/5)
1. What does driver.window_handles return in Selenium?
easy
A. The size of the browser window
B. The title of the current browser window
C. The URL of the current page
D. A list of all open browser window handles

Solution

  1. Step 1: Understand the purpose of driver.window_handles

    This command returns all the unique IDs (handles) of open browser windows or tabs controlled by Selenium.
  2. Step 2: Differentiate from other window properties

    It does not return titles, URLs, or sizes, only handles to switch between windows.
  3. Final Answer:

    A list of all open browser window handles -> Option D
  4. Quick Check:

    window_handles = list of handles [OK]
Hint: window_handles gives all open windows as a list [OK]
Common Mistakes:
  • Confusing window_handles with current window title
  • Expecting a single handle instead of a list
  • Using window_handles to get URL or size
2. Which of the following is the correct syntax to switch to a new window using Selenium in Python?
easy
A. driver.switch.window(new_window_handle)
B. driver.switch_to.window(new_window_handle)
C. driver.switchTo().window(new_window_handle)
D. driver.switch_window(new_window_handle)

Solution

  1. Step 1: Recall Selenium Python syntax for switching windows

    The correct method is driver.switch_to.window(handle) to switch context to the window with the given handle.
  2. Step 2: Identify incorrect syntax options

    Options B, C, and D use incorrect method names or Java-like syntax not valid in Python Selenium.
  3. Final Answer:

    driver.switch_to.window(new_window_handle) -> Option B
  4. Quick Check:

    Use switch_to.window() in Python Selenium [OK]
Hint: Use driver.switch_to.window(handle) to switch windows [OK]
Common Mistakes:
  • Using Java syntax like switchTo() in Python
  • Missing '_to' in switch_to
  • Using non-existent methods like switch_window
3. Given the following code snippet, what will be printed?
original_handle = driver.current_window_handle
all_handles = driver.window_handles
for handle in all_handles:
    if handle != original_handle:
        driver.switch_to.window(handle)
        print(driver.title)
        break
medium
A. The title of the original window
B. No output because loop never runs
C. The title of the new window (not original)
D. An error because of wrong handle usage

Solution

  1. Step 1: Understand the code logic

    The code saves the original window handle, then loops through all handles. It switches to the first handle that is not the original.
  2. Step 2: Determine what is printed

    After switching, it prints the title of the new window and breaks the loop, so it prints the new window's title.
  3. Final Answer:

    The title of the new window (not original) -> Option C
  4. Quick Check:

    Switch to non-original window, print title [OK]
Hint: Switch to non-original handle to get new window title [OK]
Common Mistakes:
  • Expecting original window title to print
  • Confusing current_window_handle with window_handles
  • Thinking loop does not run
4. What is wrong with this code snippet for switching to a new window?
new_window = driver.window_handles[1]
driver.switch_to.window(new_window)
driver.close()
medium
A. Index 1 may not exist if only one window is open
B. switch_to.window() is not a valid method
C. It closes the new window instead of switching back
D. driver.close() closes all windows

Solution

  1. Step 1: Analyze window_handles indexing

    Accessing index 1 assumes there are at least two windows open. If only one window exists, index 1 causes an IndexError.
  2. Step 2: Check other statements

    switch_to.window() is valid, driver.close() closes only current window, so no issue there.
  3. Final Answer:

    Index 1 may not exist if only one window is open -> Option A
  4. Quick Check:

    Check window_handles length before indexing [OK]
Hint: Always check window_handles length before indexing [OK]
Common Mistakes:
  • Assuming multiple windows always exist
  • Thinking driver.close() closes all windows
  • Confusing method names
5. You open a new tab in Selenium and want to switch to it, but you don't know its handle. Which code snippet correctly switches to the new tab?
hard
A. new_handle = [h for h in driver.window_handles if h != driver.current_window_handle][0] driver.switch_to.window(new_handle)
B. driver.switch_to.window(driver.current_window_handle + 1)
C. driver.switch_to.window(driver.window_handles[-1])
D. driver.switch_to.window(driver.window_handles[0])

Solution

  1. Step 1: Understand the problem

    You want to switch to the new tab but only know the current window handle, not the new one.
  2. Step 2: Analyze each option

    new_handle = [h for h in driver.window_handles if h != driver.current_window_handle][0] driver.switch_to.window(new_handle) finds the handle that is not the current one and switches to it correctly.
    driver.switch_to.window(driver.current_window_handle + 1) tries to add 1 to a string handle, causing error.
    driver.switch_to.window(driver.window_handles[-1]) switches to the last handle, which may work but is unsafe if multiple windows exist.
    driver.switch_to.window(driver.window_handles[0]) switches back to the original window, not the new tab.
  3. Final Answer:

    Use list comprehension to find new handle and switch -> Option A
  4. Quick Check:

    Find new handle by exclusion, then switch [OK]
Hint: Find new handle by excluding current, then switch [OK]
Common Mistakes:
  • Adding 1 to a string handle (invalid)
  • Assuming last handle is always new
  • Switching to original window by mistake
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