How to Close a Browser in Selenium

Test your website on different browsers using Selenium Browser Automation. Know how to close a browser in automated Selenium tests.

Get Started free
Home Guide How to Close a Browser in Selenium

How to Close a Browser in Selenium

Closing a browser correctly is a key part of reliable Selenium test automation. Whether you’re running a single test or a full suite, knowing when to use close() vs quit() can help prevent resource leaks and unexpected test behavior.

Overview

What is close() in Selenium?

Close() in Selenium closes the currently active browser window. If it’s the only window open, it also ends the WebDriver session.

What is quit() in Selenium?

Quit() in Selenium closes all browser windows managed by the WebDriver instance and ends the entire session, similar to calling dispose() on the WebDriver.

When to use which:

  • Use close() to close a single window and potentially keep the session active.
  • Use quit() to close all windows and end the session completely.

This article explains the right way to close browsers in Selenium and breaks down the difference between these two commonly used methods.

What is Selenium?

Selenium is an open-source automation testing framework for web applications. It supports multiple browsers and platforms, allowing test scripts to be written in languages such as Java, Python, C#, and JavaScript.

With Selenium, testers can automate user interactions, validate page behavior, and run tests across different environments. Its core tools include Selenium WebDriver for browser automation, Selenium IDE for codeless test creation, and Selenium Grid for parallel test execution.

To run Selenium tests reliably across real devices and browsers without managing infrastructure, teams often use cloud platforms like BrowserStack. It provides instant access to thousands of real browser and device combinations, helping ensure consistent test coverage and faster releases.

BrowserStack Automate Banner

How to Close a Browser in Selenium

To close a browser using Selenium, simply use the close() method. Refer to the code below as an example:

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

public class CloseBrowserExample{
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "Path_to_Chrome_Driver");
WebDriver driver=new ChromeDriver();
driver.close(); //closes the browser
}
}

driver.close() and driver.quit() are two methods for closing a browser session in Selenium WebDriver. It is necessary to know when to use each method in a test script.

  • driver.close() closes only the current window on which Selenium is running automated tests. The WebDriver session, however, remains active.
  • On the other hand, the driver.quit() method closes all browser windows and ends the WebDriver session.

Note that if no other window is open and the script calls driver.close() then it will also close the current WebDriver session.

Here arises the question: when should a tester use which method?

Here’s an example to understand how to call both these methods in Selenium.

driver.close()

This method is used when you want to close the current existing window you are working on. Please note driver.close() will not terminate the browser instance.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
public class CloseWindow {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "Path to Chrome Driver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
//implicit wait
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.findElement(By.name(“q")).sendkeys(Selenium);
driver.findElement(By.name(“btnk")).click();
// window handles
Set w = driver.getWindowHandles();
// window handles iterate
Iterator t = w.iterator();
String h = (String) t.next();
String p = (String) t.next();
// switching child window
driver.switchTo().window(h);
// close only the child browser window
driver.close();
}
}’

On executing the above code, output goes as follows:

Talk to an Expert

driver.quit()

This method is used when you want to terminate the webdriver instance window you are working on. Please note driver.quit() will terminate the browser instance.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
public class CloseWindow {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "Path to Chrome Driver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
//implicit wait
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.findElement(By.name(“q")).sendkeys(Selenium);
driver.findElement(By.name(“btnk")).click();
// window handles
Set w = driver.getWindowHandles();
// window handles iterate
Iterator t = w.iterator();
String h = t.next();
String p = t.next();
// switching child window
driver.switchTo().window(h);
// switching parent window
driver.switchTo().window(p);
// terminates driver session and closes all windows
driver.quit();
}
}

Difference Between close() and quit() in Selenium

In Selenium WebDriver, both close() and quit() are used to end browser sessions, but they behave differently:

driver.close()

This method closes the current active browser window that the WebDriver is controlling. If multiple tabs or windows are open, only the focused one will be closed. The WebDriver session remains active unless it is the last window.

driver.quit()

This method closes all browser windows opened during the WebDriver session and ends the session entirely. It also releases the system resources associated with the driver instance.

Key Difference

  • Use close() when only one tab or window needs to be closed.
  • Use quit() to completely exit the browser and clean up the WebDriver instance.

Choosing the appropriate method ensures better test reliability and avoids resource leaks during automation.

Conclusion

Closing the browser properly is a critical step in Selenium automation to ensure clean test execution and optimal resource management. Understanding the difference between driver.close() and driver.quit() helps avoid common issues like leaving background browser processes running or unintentionally ending a test session too early. By using these methods correctly, teams can maintain stable and efficient test scripts.

For seamless Selenium testing across real browsers and devices without the hassle of infrastructure setup, platforms like BrowserStack offer a reliable and scalable solution.

Selenium tests deliver the most accurate results when run on real devices. With BrowserStack’s real device cloud, tests can be executed on over 3500+ real browsers and devices.

Leverage parallel testing on a cloud Selenium Grid to speed up test execution without sacrificing accuracy. Identify bugs early by testing software under real user conditions using BrowserStack.

Tags
Automation Testing Selenium Selenium Webdriver

Get answers on our Discord Community

Join our Discord community to connect with others! Get your questions answered and stay informed.

Join Discord Community
Discord