</>

Technology

Selenium

Difficulty

Beginner

Interview Question

What is the difference between driver.close() and driver.quit() in Selenium?

driver.close() closes only the current active window; driver.quit() closes all open windows and ends the WebDriver session.

Answer

MethodWhat it does
driver.close()Closes the current/active browser window only
driver.quit()Closes all browser windows and ends the entire WebDriver session

driver.close():

Java
driver.close();
// Only closes the currently focused window
// If this was the last window, session ends but driver object remains

driver.quit():

Java
driver.quit();
// Closes all windows (main + all popups/tabs) and kills the driver process

Example with multiple windows:

Java
String mainHandle = driver.getWindowHandle();
driver.switchTo().window(popupHandle);

driver.close();                          // Only closes popup
driver.switchTo().window(mainHandle);   // Main window is still open

// At end of test:
driver.quit();                           // Closes everything

When to use:

  • Use driver.close() in the middle of a test when closing a specific window/tab
  • Use driver.quit() in @AfterMethod or teardown — always prefer quit() to fully clean up

Best practice: Always call driver.quit() in a finally block or @AfterMethod to prevent browser processes from lingering in memory.

Follow AutomateQA

Related Topics