0
0
Selenium Pythontesting~20 mins

Closing browser (close vs quit) in Selenium Python - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Browser Closing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What happens after calling driver.close() with multiple tabs?

Consider a Selenium WebDriver session with 3 browser tabs open. What is the state of the browser after driver.close() is called once?

Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

service = Service()
driver = webdriver.Chrome(service=service)
driver.get('https://example.com')
driver.execute_script("window.open('https://example.org', '_blank');")
driver.execute_script("window.open('https://example.net', '_blank');")

# Current tabs: 3

driver.close()

# What happens next?
AAn exception is thrown because close() cannot be called with multiple tabs open.
BAll browser tabs are closed and the WebDriver session ends.
CThe browser window is minimized but all tabs remain open.
DOnly the current tab is closed; the other two tabs remain open and the WebDriver session continues.
Attempts:
2 left
💡 Hint

Think about what close() does compared to quit().

assertion
intermediate
1:30remaining
Which assertion correctly verifies the browser is closed after driver.quit()?

After calling driver.quit(), which assertion correctly checks that the browser session is closed?

Aassert driver.session_id is None
Bassert driver.current_url == ''
Cassert driver.title == ''
Dassert driver.window_handles == []
Attempts:
2 left
💡 Hint

Check what happens to the session ID after quitting.

🔧 Debug
advanced
2:00remaining
Why does calling driver.close() twice cause an error?

Given this code snippet:

driver.get('https://example.com')
driver.close()
driver.close()

Why does the second driver.close() call raise an exception?

AThe second close() call raises a WebDriverException because the driver object is None.
BThe browser window is already closed after the first call, so the second call tries to close a non-existent window causing a NoSuchWindowException.
CThe second close() call causes a TimeoutException because the browser is busy closing.
DCalling close() twice is allowed and does not raise any exception.
Attempts:
2 left
💡 Hint

Think about what happens to the browser window after the first close.

🧠 Conceptual
advanced
1:30remaining
Difference between driver.close() and driver.quit() in Selenium

Which statement best describes the difference between driver.close() and driver.quit()?

A<code>driver.close()</code> closes the current browser window or tab, while <code>driver.quit()</code> closes all browser windows and ends the WebDriver session.
B<code>driver.close()</code> ends the WebDriver session, while <code>driver.quit()</code> only closes the current tab.
C<code>driver.close()</code> minimizes the browser window, and <code>driver.quit()</code> closes the current tab.
D<code>driver.close()</code> refreshes the current page, and <code>driver.quit()</code> closes the browser window.
Attempts:
2 left
💡 Hint

Think about session lifecycle and window management.

framework
expert
2:30remaining
Best practice to ensure browser closes after test failure in Selenium with pytest

In a pytest test using Selenium WebDriver, which approach best ensures the browser closes even if the test fails?

ACall <code>driver.quit()</code> only inside the test function after assertions.
BCall <code>driver.close()</code> at the end of the test function only.
CUse a pytest fixture with <code>yield</code> to initialize the driver and call <code>driver.quit()</code> after the test, regardless of outcome.
DRely on the browser to close automatically when the Python script ends.
Attempts:
2 left
💡 Hint

Think about pytest fixtures and cleanup behavior.