0
0
Selenium Pythontesting~20 mins

Handling multiple pages in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Pages 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 code that opens a new tab and switches to it. What will be printed?
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

browser = webdriver.Chrome()
browser.get('https://example.com')

original_window = browser.current_window_handle
browser.execute_script("window.open('https://example.org', '_blank');")
time.sleep(1)

for handle in browser.window_handles:
    if handle != original_window:
        browser.switch_to.window(handle)
        break

print(browser.title)
browser.quit()
ANoSuchWindowException
BExample Domain - example.org
CExample Domain
Dselenium.common.exceptions.WebDriverException
Attempts:
2 left
💡 Hint
Remember that the new tab opens 'https://example.org' and the title of that page is 'Example Domain'.
assertion
intermediate
1:30remaining
Which assertion correctly verifies the switch to a new window?
You have switched to a new window in 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 new window and switch to it

# Which assertion 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
After switching, the current window handle should be different from the original.
locator
advanced
2:00remaining
Which locator strategy is best to find a link that opens a new tab?
You want to find a link that opens a new tab when clicked. The link has text 'Open New Tab' and attribute target='_blank'. Which locator is best?
Adriver.find_element(By.XPATH, '//a[@target="_blank" and text()="Open New Tab"]')
Bdriver.find_element(By.CSS_SELECTOR, 'a[target="_blank"]')
Cdriver.find_element(By.LINK_TEXT, 'Open New Tab')
Ddriver.find_element(By.ID, 'open-new-tab')
Attempts:
2 left
💡 Hint
Use a locator that matches both the attribute and the visible text for precision.
🔧 Debug
advanced
2:30remaining
Why does this Selenium code raise NoSuchWindowException?
Review the code below. Why does it raise NoSuchWindowException when trying to switch windows?
Selenium Python
driver.get('https://example.com')
original = driver.current_window_handle
# Open new tab
driver.execute_script("window.open('https://example.org');")
# Incorrect switch
for handle in driver.window_handles:
    if handle == original:
        driver.switch_to.window(handle)
        break

# Trying to interact with new tab here
AThe code switches back to the original window instead of the new one
BThe new window handle is not in driver.window_handles
CThe driver.execute_script syntax is invalid
DThe browser does not support multiple tabs
Attempts:
2 left
💡 Hint
Check which window handle the code switches to.
framework
expert
3:00remaining
In a Selenium test framework, how to best handle multiple tabs to ensure tests are reliable?
You design a Selenium test framework that opens multiple tabs during tests. Which practice best ensures tests remain reliable and maintainable?
AKeep all tabs open and switch randomly between them as needed
BOpen all tabs at the start and never close any during tests
CUse time.sleep() to wait for tabs to load before switching
DAlways switch back to the original window after each tab interaction and close extra tabs
Attempts:
2 left
💡 Hint
Think about test isolation and resource cleanup.