0
0
Selenium Pythontesting~20 mins

Window handles in Selenium Python - Practice Problems & Coding Challenges

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