Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:SeleniumHQ Selenium WebDriver Quit

From Leeroopedia
Knowledge Sources
Domains Browser_Automation, WebDriver, Resource_Management
Last Updated 2026-02-11 00:00 GMT

Overview

Concrete tool for terminating a WebDriver session and releasing all associated resources provided by the Selenium WebDriver Java interface.

Description

The WebDriver.quit() and WebDriver.close() methods handle session termination. quit() sends an HTTP DELETE /session/{id} command, which closes all browser windows, terminates the browser process, stops the driver service, and invalidates the session. close() only closes the current window, ending the session only if it was the last open window. In ChromiumDriver, the quit() method is overridden to call super.quit() which delegates to RemoteWebDriver.quit() for the actual HTTP command and service shutdown.

Usage

Call quit() in test teardown (e.g., @AfterEach in JUnit 5) to ensure clean resource release. Call close() when working with multiple windows and you want to close only the current one without ending the session.

Code Reference

Source Location

  • Repository: Selenium
  • File: java/src/org/openqa/selenium/WebDriver.java
  • Lines: L147-156 (close, quit interface methods)
  • Implementation: java/src/org/openqa/selenium/chromium/ChromiumDriver.java (L420-422)

Signature

// WebDriver interface
public interface WebDriver extends SearchContext {
    /**
     * Close the current window, quitting the browser if it's the last window currently open.
     */
    void close();

    /**
     * Quits this driver, closing every associated window.
     */
    void quit();
}

// ChromiumDriver implementation
public class ChromiumDriver extends RemoteWebDriver {
    @Override
    public void quit() {
        super.quit();
    }
}

Import

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;

I/O Contract

Inputs

Name Type Required Description
(none) - - Called on an active WebDriver session instance

Outputs

Name Type Description
(void) void Browser process terminated, driver service stopped, session invalidated

Usage Examples

Proper Cleanup with try-finally

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

WebDriver driver = new ChromeDriver();
try {
    driver.get("https://example.com");
    // ... test operations
} finally {
    driver.quit(); // Always clean up
}

JUnit 5 Test Lifecycle

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

class MyWebTest {
    private WebDriver driver;

    @BeforeEach
    void setUp() {
        driver = new ChromeDriver();
    }

    @AfterEach
    void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

    @Test
    void testPageTitle() {
        driver.get("https://example.com");
        // assertions...
    }
}

Close Single Window

import org.openqa.selenium.WindowType;

// Store original window handle
String originalHandle = driver.getWindowHandle();

// Open a new window
driver.switchTo().newWindow(WindowType.WINDOW);
driver.get("https://example.com/second");

// Close only the current window (session continues with other windows)
driver.close();

// Switch back to original window
driver.switchTo().window(originalHandle);

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment