Closing the Browser

Introduction

After completing an automation test, it is important to close the browser properly. Closing the browser releases system resources, ends the WebDriver session, and ensures that no unnecessary browser instances remain running.

Selenium WebDriver provides the quit() method to close the browser. Unlike close(), which closes only the current browser window, quit() closes all browser windows opened by Selenium and completely ends the WebDriver session.

Using driver.quit() at the end of every Selenium script is considered a best practice and is widely followed in automation frameworks.

In this tutorial, you will learn how to close the browser using Selenium WebDriver, understand the code, explore real-world applications, common mistakes, best practices, and frequently asked interview questions.


What is driver.quit()?

The quit() method closes all browser windows associated with the current WebDriver session and completely terminates the session.

When driver.quit() is executed:

  • All browser windows are closed.

  • The WebDriver session ends.

  • Browser resources are released.

  • The automation script finishes cleanly.


Why Do We Use driver.quit()?

The quit() method is used because it:

  • Closes every browser window opened by Selenium.

  • Ends the WebDriver session.

  • Releases memory and system resources.

  • Prevents orphan browser processes.

  • Ensures clean execution of automation tests.


Syntax

driver.quit()

Practical Example

The following example launches Chrome, opens the Selenium practice website, stores the current WebDriver session ID, closes the browser using quit(), and verifies that the session was successfully created.

from selenium import webdriver


# Topic: 4. Your First Selenium Script - Closing the Browser
# Practice site: https://the-internet.herokuapp.com/
#
# driver.quit() ends the entire browser session and releases all resources.
# Always call quit() in a finally block so the browser closes even if a test fails.


def test_closing_browser_with_quit():
    driver = webdriver.Chrome()
    driver.get("https://the-internet.herokuapp.com/")

    session_id = driver.session_id
    driver.quit()

    assert session_id is not None

Output

Chrome browser launched successfully.

Website opened successfully.

Browser session closed successfully.

WebDriver session terminated.

Assertion Passed.

Test Executed Successfully.

Understanding the Code

Import Selenium WebDriver

from selenium import webdriver

Imports the Selenium WebDriver module required for browser automation.


Launch Chrome Browser

driver = webdriver.Chrome()

Starts a new Chrome browser session.


Open the Website

driver.get("https://the-internet.herokuapp.com/")

Navigates to the Selenium practice website.


Store the Session ID

session_id = driver.session_id

Stores the unique WebDriver session ID before closing the browser.

This confirms that the browser session was successfully created.


Close the Browser

driver.quit()

Closes every browser window and completely ends the WebDriver session.

After this statement executes, the driver object can no longer be used.


Verify the Session Was Created

assert session_id is not None

Verifies that a valid WebDriver session existed before the browser was closed.


Execution Flow

Import Selenium
        │
        ▼
Launch Chrome Browser
        │
        ▼
Open Website
        │
        ▼
Create WebDriver Session
        │
        ▼
Store Session ID
        │
        ▼
Close Browser Using quit()
        │
        ▼
End WebDriver Session

Automation Testing Example

Suppose you automate a login feature.

After verifying that the user logs in successfully, the browser should be closed to prepare for the next test.

driver.get("https://company-app.com/login")

driver.quit()

This ensures that each test starts with a fresh browser session.


Real-World Example

In an e-commerce automation framework, hundreds of test cases may execute one after another.

At the end of every test case, Selenium closes the browser using:

driver.quit()

This prevents multiple Chrome processes from remaining in memory and improves system performance.


Common Mistakes Beginners Make

Using close() Instead of quit()

Incorrect

driver.close()

close() only closes the current browser window.

If multiple browser windows are open, the WebDriver session remains active.


Correct

driver.quit()

quit() closes all browser windows and completely ends the WebDriver session.


Trying to Use the Driver After quit()

Incorrect

driver.quit()

driver.title

This raises a NoSuchSessionException because the session has already ended.


Forgetting to Close the Browser

Incorrect

driver = webdriver.Chrome()

driver.get("https://the-internet.herokuapp.com/")

The browser remains open after execution, consuming system resources.


Correct

driver.quit()

Always close the browser after completing the test.


Best Practices

  • Always use driver.quit() at the end of every Selenium script.

  • Prefer quit() over close() when ending a test.

  • Place driver.quit() inside a finally block to ensure the browser closes even if an exception occurs.

  • Avoid using the driver after calling quit().

  • Close every WebDriver session to prevent unnecessary browser processes.


Conclusion

The driver.quit() method is the recommended way to close the browser in Selenium. It closes all browser windows, terminates the WebDriver session, and releases system resources.

Using quit() consistently ensures clean, reliable, and efficient automation execution. It is a fundamental practice that every Selenium automation engineer should follow.


Frequently Asked Questions (FAQs)

What does driver.quit() do?

It closes all browser windows and completely ends the WebDriver session.


What is the syntax for closing the browser?

driver.quit()

What is the difference between close() and quit()?

  • close() closes only the current browser window.

  • quit() closes all browser windows and terminates the entire WebDriver session.


Why should driver.quit() be placed inside a finally block?

It ensures the browser is closed even if an exception occurs during test execution.


Can I use the driver after calling driver.quit()?

No.

Once driver.quit() is executed, the WebDriver session ends and the driver object becomes invalid.


Key Takeaways

  • driver.quit() closes all browser windows and completely ends the WebDriver session.

  • It is the recommended method for closing the browser after test execution.

  • quit() releases browser resources and prevents orphan browser processes.

  • Always prefer quit() over close() when ending an automation test.

  • Place driver.quit() inside a finally block for reliable cleanup.

  • Do not use the driver after calling quit().

  • Properly closing the browser is a best practice in Selenium automation.

  • Understanding driver.quit() is essential before learning advanced browser management and automation framework development.