Challenge - 5 Problems
Window and Tab Mastery
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
len(driver.window_handles) after execution?Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys options = webdriver.ChromeOptions() options.add_argument('--headless=new') driver = webdriver.Chrome(options=options) driver.get('https://example.com') # Open a new tab original_windows = driver.window_handles body = driver.find_element(By.TAG_NAME, 'body') body.send_keys(Keys.CONTROL + 't') # Switch to new tab new_windows = driver.window_handles print(len(new_windows)) driver.quit()
Attempts:
2 left
💡 Hint
Remember that opening a new tab adds one window handle to the list.
✗ Incorrect
Initially, there is one window handle. After sending Ctrl+T, a new tab opens, increasing the count to 2.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies the new window title after opening a new window?
You have opened a new window using Selenium and switched to it. The new window loads 'https://www.python.org'. Which assertion correctly checks that the new window's title contains 'Python'?
Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By options = webdriver.ChromeOptions() options.add_argument('--headless=new') driver = webdriver.Chrome(options=options) driver.get('https://example.com') # Open new window driver.execute_script("window.open('https://www.python.org', '_blank');") # Switch to new window new_window = driver.window_handles[-1] driver.switch_to.window(new_window) # Assertion here
Attempts:
2 left
💡 Hint
Check Python string methods for containment.
✗ Incorrect
The correct way to check if a substring is in a string is using the 'in' keyword. Option A uses this correctly.
❓ locator
advanced2:00remaining
Which locator is best to find the button that opens a new window?
You want to locate a button that opens a new window. The button has the attribute
aria-label="Open new window". Which locator is best practice to find this button in Selenium?Attempts:
2 left
💡 Hint
Use attribute selectors for accessibility attributes.
✗ Incorrect
Using CSS selector with the aria-label attribute is both semantic and efficient. Option B uses this best practice.
🔧 Debug
advanced2:00remaining
Why does this Selenium code fail to switch to the new tab?
The code below opens a new tab but fails to switch to it. What is the cause?
Selenium Python
driver.get('https://example.com') original_windows = driver.window_handles # Open new tab driver.execute_script("window.open('https://www.google.com', '_blank');") # Attempt to switch driver.switch_to.window(original_windows[1])
Attempts:
2 left
💡 Hint
Check when the window handles list is captured.
✗ Incorrect
The list original_windows was captured before opening the new tab, so it only has one handle at index 0. Index 1 is out of range.
❓ framework
expert3:00remaining
Which pytest fixture setup correctly handles opening and closing a new browser window for tests?
You want a pytest fixture that opens a Chrome browser, yields the driver for tests, and ensures the browser closes after tests. Which fixture code is correct?
Attempts:
2 left
💡 Hint
Remember that code after yield runs as teardown.
✗ Incorrect
Option D correctly creates the driver, yields it for tests, and quits after tests. Option D quits before yield, so driver is closed during tests. Option D calls close() which closes only current window, not entire browser. Option D is missing @pytest.fixture decorator.