Consider a Selenium WebDriver session with 3 browser tabs open. What is the state of the browser after driver.close() is called once?
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?
Think about what close() does compared to quit().
driver.close() closes only the current active tab or window. If multiple tabs are open, the others remain active and the WebDriver session continues. driver.quit() closes all tabs and ends the session.
After calling driver.quit(), which assertion correctly checks that the browser session is closed?
Check what happens to the session ID after quitting.
After driver.quit(), the WebDriver session ends and driver.session_id becomes None. Other properties like current_url or title will raise exceptions because the browser is closed.
Given this code snippet:
driver.get('https://example.com')
driver.close()
driver.close()Why does the second driver.close() call raise an exception?
Think about what happens to the browser window after the first close.
After the first driver.close(), the browser window is closed. The second call tries to close a window that no longer exists, causing a NoSuchWindowException.
Which statement best describes the difference between driver.close() and driver.quit()?
Think about session lifecycle and window management.
driver.close() closes only the active window or tab, but the WebDriver session remains active if other windows are open. driver.quit() closes all windows and ends the session.
In a pytest test using Selenium WebDriver, which approach best ensures the browser closes even if the test fails?
Think about pytest fixtures and cleanup behavior.
Using a pytest fixture with yield allows setup before the test and guaranteed cleanup after, even if the test fails or raises an exception. Calling driver.quit() in the fixture's teardown ensures the browser closes properly.