0
0
Selenium Pythontesting~20 mins

Switching between windows in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Window Switching 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 window switch code?

Consider the following Selenium Python code snippet that opens a new window and switches to it. What will be the printed output?

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# Setup driver
options = webdriver.ChromeOptions()
options.add_argument('--headless=new')
driver = webdriver.Chrome(options=options)

# Open initial page
driver.get('https://example.com')
original_window = driver.current_window_handle

# Open new window
driver.execute_script('window.open("https://example.org", "_blank");')
time.sleep(1)

# Switch to new window
for handle in driver.window_handles:
    if handle != original_window:
        driver.switch_to.window(handle)
        break

print(driver.title)
driver.quit()
ANoSuchWindowException
BExample Domain - New Window
CExample Domain - Example Org
DExample Domain
Attempts:
2 left
💡 Hint

Remember that driver.title returns the title of the current page after switching windows.

assertion
intermediate
1:30remaining
Which assertion correctly verifies the switch to a new window?

You have switched to a new browser window using Selenium. Which assertion correctly checks that the current window handle is not the original one?

Selenium Python
original_window = driver.current_window_handle
# code to open and switch to new window

# Which assertion below is correct?
Aassert driver.current_window_handle != original_window
Bassert driver.current_window_handle == original_window
Cassert driver.window_handles == original_window
Dassert driver.current_window_handle in original_window
Attempts:
2 left
💡 Hint

The current window handle should be different from the original after switching.

locator
advanced
1:30remaining
Which locator strategy is best to find the button that opens a new window?

On a webpage, a button opens a new window when clicked. The button has the text "Open New Window" and a unique id "open-window-btn". Which locator is best to find this button in Selenium?

Adriver.find_element(By.CSS_SELECTOR, 'button#open-window-btn')
Bdriver.find_element(By.XPATH, "//button[text()='Open New Window']")
Cdriver.find_element(By.ID, 'open-window-btn')
Ddriver.find_element(By.CLASS_NAME, 'open-window-btn')
Attempts:
2 left
💡 Hint

Using unique IDs is the fastest and most reliable locator.

🔧 Debug
advanced
2:00remaining
Why does this Selenium code raise NoSuchWindowException?

Review the code below. It tries to switch to a new window but raises NoSuchWindowException. What is the likely cause?

Selenium Python
original_window = driver.current_window_handle
# Open new window
driver.execute_script('window.open("https://example.org", "_blank");')

# Incorrect switch
handle = driver.window_handles[1]
driver.switch_to.window(handle)

# Then close original window
driver.switch_to.window(original_window)
driver.close()
AThe original window was closed before switching back to it
BThe driver.execute_script syntax is invalid
CThe new window handle index is wrong
DThe driver.switch_to.window method is deprecated
Attempts:
2 left
💡 Hint

Check the order of window closing and switching.

framework
expert
3:00remaining
How to implement a robust window switch helper in Selenium Python?

You want to create a helper function that waits for a new window to open and switches to it safely. Which implementation below correctly waits up to 10 seconds for a new window and switches to it?

Selenium Python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def switch_to_new_window(driver, timeout=10):
    original_windows = driver.window_handles
    WebDriverWait(driver, timeout).until(
        lambda d: len(d.window_handles) > len(original_windows)
    )
    new_windows = set(driver.window_handles) - set(original_windows)
    driver.switch_to.window(new_windows.pop())
AThe code uses deprecated Selenium methods
BThe code waits for a new window and switches to it correctly
CThe code switches before waiting for the new window
DThe code waits but does not switch to the new window
Attempts:
2 left
💡 Hint

Check the order of waiting and switching in the function.