Challenge - 5 Problems
Window Handles Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Think about which window handle is active after switching.
✗ Incorrect
The code opens a new tab and switches to it. The
current_handle will be the new tab's handle, which is different from original_handle. So the comparison returns False.❓ assertion
intermediate1: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?Attempts:
2 left
💡 Hint
Check the current window handle after switching back.
✗ Incorrect
The current_window_handle property gives the active window's handle. To confirm switching back, it must equal the original handle.
❓ locator
advanced1: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?
Attempts:
2 left
💡 Hint
The link text is unique and exact.
✗ Incorrect
Using LINK_TEXT with the exact visible text is the most direct and reliable way to find the link by its text.
🔧 Debug
advanced2: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')
Attempts:
2 left
💡 Hint
Check if the handle you switch to is valid.
✗ Incorrect
Switching to a window handle that is not in the list of open windows causes NoSuchWindowException.
❓ framework
expert2: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?
Attempts:
2 left
💡 Hint
Think about cleaning all opened windows to avoid test interference.
✗ Incorrect
Closing all windows explicitly in teardown prevents leftover windows affecting other tests and ensures clean state.