What Selenium method is used to open a new browser tab or window?
The driver.switch_to.new_window() method is used to open a new tab or window in Selenium with Python.
Click to reveal answer
beginner
How do you switch control to a newly opened tab or window in Selenium?
Use driver.switch_to.window(window_handle) where window_handle is the unique ID of the new tab or window.
Click to reveal answer
intermediate
What is the difference between opening a new tab and a new window in Selenium?
Using driver.switch_to.new_window('tab') opens a new tab, while driver.switch_to.new_window('window') opens a new browser window.
Click to reveal answer
beginner
Why is it important to keep track of window handles when working with multiple tabs/windows?
Window handles let you identify and switch between different tabs or windows. Without them, Selenium cannot control the right browser context.
Click to reveal answer
intermediate
How can you close a specific tab or window in Selenium?
Switch to the tab/window using its handle, then call driver.close() to close it. Use driver.switch_to.window() before closing.
Click to reveal answer
Which Selenium method opens a new browser tab?
Adriver.switch_to.new_window('tab')
Bdriver.open_new_tab()
Cdriver.create_tab()
Ddriver.new_tab()
✗ Incorrect
The correct method is driver.switch_to.new_window('tab') to open a new tab.
How do you switch control to a new window in Selenium?
Adriver.switch_to.window(window_handle)
Bdriver.switch_to.new_window('window')
Cdriver.switch_to.tab(window_handle)
Ddriver.switch_window(window_handle)
✗ Incorrect
Use driver.switch_to.window(window_handle) to switch control to an existing window.
What does driver.close() do in Selenium?
ACloses the browser driver
BCloses all browser windows
CCloses the current tab or window
DCloses the Selenium session
✗ Incorrect
driver.close() closes only the current tab or window Selenium is controlling.
Why do you need window handles in Selenium?
ATo open new tabs
BTo refresh the page
CTo close the browser
DTo identify and switch between multiple tabs or windows
✗ Incorrect
Window handles uniquely identify each tab or window so Selenium can switch control.
Which argument opens a new browser window instead of a tab?
A'new'
B'window'
C'tab'
D'browser'
✗ Incorrect
Use driver.switch_to.new_window('window') to open a new browser window.
Explain how to open a new tab, switch to it, and then close it using Selenium with Python.
Think about the sequence: open, switch, close.
You got /3 concepts.
Describe why managing window handles is important when working with multiple browser tabs or windows in Selenium.
Consider how Selenium knows which tab or window to work with.
You got /3 concepts.
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
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'.
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').
Final Answer:
driver.switch_to.new_window('tab') -> Option A
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
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.
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.
Final Answer:
driver.switch_to.new_window('window') -> Option B
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?
Calling driver.new_window('tab') directly is invalid and will raise an AttributeError.
Final Answer:
Missing driver.switch_to before new_window() -> Option C
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
Step 1: Save original window handle before opening new tab
Store driver.current_window_handle in a variable to switch back later.
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().
Step 3: Switch back to original tab
Use driver.switch_to.window(original) to return focus to the original tab.
Final Answer:
Code snippet D correctly opens a new tab, loads URL, and switches back -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.