0
0
Selenium Pythontesting~15 mins

Closing browser (close vs quit) in Selenium Python - Automation Approaches Compared

Choose your learning style9 modes available
Verify browser window closing behavior using close() and quit()
Preconditions (3)
Step 1: Open the browser using Selenium WebDriver
Step 2: Navigate to 'https://example.com'
Step 3: Open a new browser window or tab
Step 4: Switch to the new window or tab
Step 5: Call driver.close() to close the current window
Step 6: Verify that the other window is still open
Step 7: Switch back to the original window
Step 8: Call driver.quit() to close all browser windows and end the session
Step 9: Verify that the browser is completely closed
✅ Expected Result: After driver.close(), only the current window is closed and other windows remain open. After driver.quit(), all browser windows are closed and the WebDriver session ends.
Automation Requirements - Selenium with Python
Assertions Needed:
Assert that after driver.close(), the number of open windows decreases by one
Assert that after driver.quit(), no browser windows remain open
Assert that switching to closed window raises an exception
Best Practices:
Use explicit waits to ensure pages load before actions
Use try-except blocks to handle exceptions when switching windows
Use driver.window_handles to manage multiple windows
Clean up by quitting the driver at the end of the test
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchWindowException
import time

# Setup WebDriver (adjust path to chromedriver as needed)
service = Service()
driver = webdriver.Chrome(service=service)

try:
    # Step 1 & 2: Open browser and navigate
    driver.get('https://example.com')
    original_window = driver.current_window_handle

    # Step 3: Open new window using JavaScript
    driver.execute_script('window.open("https://example.com", "_blank");')
    time.sleep(1)  # Wait for new window to open

    # Step 4: Switch to new window
    windows = driver.window_handles
    assert len(windows) == 2, f'Expected 2 windows, found {len(windows)}'
    new_window = [w for w in windows if w != original_window][0]
    driver.switch_to.window(new_window)

    # Step 5: Close current window
    driver.close()

    # Step 6: Verify one window remains
    remaining_windows = driver.window_handles
    assert len(remaining_windows) == 1, f'Expected 1 window after close(), found {len(remaining_windows)}'

    # Step 7: Switch back to original window
    driver.switch_to.window(original_window)

    # Step 8: Quit driver
    driver.quit()

    # Step 9: Verify browser closed by checking exception on window_handles
    try:
        _ = driver.window_handles
        raise AssertionError('driver.window_handles should fail after quit()')
    except Exception:
        pass  # Expected exception

except Exception as e:
    driver.quit()
    raise e

This script opens a browser and navigates to example.com. It then opens a new browser tab using JavaScript and switches to it. After switching, it calls driver.close() to close only the current tab. We verify that one window remains open by checking driver.window_handles. Then, we switch back to the original window and call driver.quit() to close all browser windows and end the session. Finally, we verify that the browser is closed by attempting to access driver.window_handles, which should raise an exception.

Explicit waits are replaced by time.sleep(1) here for simplicity, but in real tests, use Selenium explicit waits. The try-except block ensures the driver quits even if an error occurs, preventing orphan browser processes.

Common Mistakes - 4 Pitfalls
Using driver.close() expecting it to close all browser windows
Not switching to the correct window before calling driver.close()
Not handling exceptions when switching to a closed window
Not quitting the driver at the end of the test
Bonus Challenge

Now add data-driven testing to open different URLs in new tabs and verify closing behavior for each.

Show Hint