Bird
Raised Fist0
Selenium Pythontesting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which Selenium Python command opens a new browser tab?
easy
A. driver.switch_to.new_window('tab')
B. driver.open_new_tab()
C. driver.create_tab()
D. driver.new_tab()

Solution

  1. Step 1: Understand Selenium's new window method

    Selenium provides driver.switch_to.new_window() to open new tabs or windows by specifying the type as 'tab' or 'window'.
  2. Step 2: Identify the correct argument for a new tab

    To open a new tab, the argument must be the string 'tab'. So the correct command is driver.switch_to.new_window('tab').
  3. Final Answer:

    driver.switch_to.new_window('tab') -> Option A
  4. Quick Check:

    Open new tab = driver.switch_to.new_window('tab') [OK]
Hint: Use 'tab' string inside new_window() to open a new tab [OK]
Common Mistakes:
  • Using non-existent methods like driver.open_new_tab()
  • Passing 'window' instead of 'tab' to open a tab
  • Trying driver.new_tab() which is invalid
2. Which of the following is the correct syntax to open a new browser window in Selenium Python?
easy
A. driver.switch_to.new_window('tab')
B. driver.switch_to.new_window('window')
C. driver.new_window('window')
D. driver.open_window('window')

Solution

  1. Step 1: Recall the method to open new windows

    Selenium's correct method is driver.switch_to.new_window() with argument 'window' to open a new window.
  2. Step 2: Check syntax correctness

    Only driver.switch_to.new_window('window') uses the correct method and argument. driver.new_window('window') and driver.open_window('window') use invalid method names, and driver.switch_to.new_window('tab') opens a tab, not a window.
  3. Final Answer:

    driver.switch_to.new_window('window') -> Option B
  4. Quick Check:

    Open new window = driver.switch_to.new_window('window') [OK]
Hint: Use 'window' string inside new_window() to open a new window [OK]
Common Mistakes:
  • Confusing 'tab' and 'window' arguments
  • Using wrong method names like driver.new_window()
  • Missing the switch_to part in the command
3. What will be the output of the following Selenium Python code snippet?
driver.get('https://example.com')
original_handle = driver.current_window_handle
driver.switch_to.new_window('tab')
driver.get('https://selenium.dev')
print(len(driver.window_handles))
medium
A. 2
B. 1
C. 0
D. Error

Solution

  1. Step 1: Analyze window handles before and after new tab

    Initially, the driver opens one window (example.com). After driver.switch_to.new_window('tab'), a new tab opens, so total windows/tabs become 2.
  2. Step 2: Count window handles

    The driver.window_handles list contains all open windows/tabs. After opening the new tab, its length is 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    New tab adds one handle, total handles = 2 [OK]
Hint: Count window_handles after new_window() to get total tabs/windows [OK]
Common Mistakes:
  • Assuming window_handles length stays 1 after new tab
  • Confusing current_window_handle with window_handles list
  • Expecting an error due to missing switch focus
4. Identify the error in this Selenium Python code that tries to open a new tab:
driver.get('https://example.com')
driver.new_window('tab')
driver.get('https://selenium.dev')
medium
A. driver.new_window() is not a valid method
B. Cannot open new tab after driver.get()
C. Missing driver.switch_to before new_window()
D. driver.get() cannot be called twice

Solution

  1. Step 1: Check method usage for opening new tab

    The correct method to open a new tab is driver.switch_to.new_window('tab'). The code misses switch_to.
  2. Step 2: Understand why missing switch_to causes error

    Calling driver.new_window('tab') directly is invalid and will raise an AttributeError.
  3. Final Answer:

    Missing driver.switch_to before new_window() -> Option C
  4. Quick Check:

    Use driver.switch_to.new_window() to open tabs/windows [OK]
Hint: Always use driver.switch_to.new_window() to open tabs/windows [OK]
Common Mistakes:
  • Calling new_window() directly on driver
  • Thinking driver.get() limits opening new tabs
  • Believing driver.get() can only be called once
5. You want to open a new tab, switch to it, load a URL, then switch back to the original tab. Which code snippet correctly does this in Selenium Python?
hard
A. original = driver.current_window_handle driver.switch_to.new_window('tab') driver.switch_to.window(original) driver.get('https://selenium.dev')
B. driver.new_window('tab') driver.get('https://selenium.dev') driver.switch_to.window(original)
C. original = driver.current_window_handle driver.switch_to.new_window('window') driver.get('https://selenium.dev') driver.switch_to.window(original)
D. original = driver.current_window_handle driver.switch_to.new_window('tab') driver.get('https://selenium.dev') driver.switch_to.window(original)

Solution

  1. Step 1: Save original window handle before opening new tab

    Store driver.current_window_handle in a variable to switch back later.
  2. Step 2: Open new tab and load URL

    Use driver.switch_to.new_window('tab') to open a new tab, then load the URL with driver.get().
  3. Step 3: Switch back to original tab

    Use driver.switch_to.window(original) to return focus to the original tab.
  4. Final Answer:

    Code snippet D correctly opens a new tab, loads URL, and switches back -> Option D
  5. Quick Check:

    Save handle, open tab, load URL, switch back [OK]
Hint: Save handle, open tab, load URL, then switch back [OK]
Common Mistakes:
  • Not saving original window handle before opening new tab
  • Using 'window' instead of 'tab' when a tab is needed
  • Switching back to original tab before loading the URL in the new tab
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard