Q12 of 37 · Selenium
What is the difference between driver.close() and driver.quit()?
Short answer
Short answer: `close()` shuts the current window/tab — the browser keeps running if other tabs exist. `quit()` ends the entire WebDriver session, kills the browser process, and frees the driver port. Use quit() in test teardown.
Detail
These two look interchangeable in a single-tab test, but they behave very differently in suites — and the wrong choice leaks browser processes that pile up in CI.
driver.close() closes the currently focused browser window. If that's the only window, the browser exits and the driver session ends naturally. If there are other open tabs/windows (because the test opened them), only the current one closes — the session remains alive but the WebDriver instance has no focused window, and subsequent calls fail with NoSuchWindowException.
driver.quit() terminates the WebDriver session: closes all windows, ends the browser process, and releases the chromedriver port. After quit(), the driver is dead — calling anything on it throws.
The rule is simple:
- In test teardown /
@AfterMethod/@AfterClass: alwaysquit(). - During a test, when you specifically want to close one tab and continue:
close()followed byswitchTo().window(otherHandle).
What goes wrong if you misuse these:
- Using
close()in teardown of a multi-tab test → browser process lingers, the next test inherits state. - Skipping teardown entirely → in CI, processes accumulate, memory exhausts, runs hang.
- Using
quit()mid-test then trying to recover → impossible; the session is gone.
A tidy pattern: always pair new ChromeDriver() (or fixture creation) with a guaranteed quit() in a finally block, @AfterMethod(alwaysRun = true), or pytest fixture teardown.