0
0
Selenium Pythontesting~20 mins

New window and tab creation in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Window and Tab Mastery
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 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()
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Remember that opening a new tab adds one window handle to the list.
assertion
intermediate
2: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
Aassert 'Python' in driver.title
Bassert driver.title == 'Python'
Cassert driver.title.contains('Python')
Dassert driver.title.index('Python')
Attempts:
2 left
💡 Hint
Check Python string methods for containment.
locator
advanced
2: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?
Adriver.find_element(By.CLASS_NAME, 'btn-open-window')
Bdriver.find_element(By.CSS_SELECTOR, 'button[aria-label="Open new window"]')
Cdriver.find_element(By.XPATH, '//button[@aria-label="Open new window"]')
Ddriver.find_element(By.ID, 'openWindowBtn')
Attempts:
2 left
💡 Hint
Use attribute selectors for accessibility attributes.
🔧 Debug
advanced
2: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])
Aswitch_to.window requires window title, not handle
Bwindow.open does not open a new tab in Selenium
Coriginal_windows list was captured before opening new tab, so index 1 does not exist
Ddriver.execute_script syntax is incorrect
Attempts:
2 left
💡 Hint
Check when the window handles list is captured.
framework
expert
3: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?
A
import pytest
from selenium import webdriver

def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()
B
import pytest
from selenium import webdriver

@pytest.fixture
def driver():
    driver = webdriver.Chrome()
    driver.quit()
    yield driver
C
import pytest
from selenium import webdriver

@pytest.fixture
def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.close()
D
import pytest
from selenium import webdriver

@pytest.fixture
def driver():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()
Attempts:
2 left
💡 Hint
Remember that code after yield runs as teardown.