In Selenium WebDriver, the close() method is used to close the current browser window that the WebDriver is controlling. If there are multiple browser windows open, the close() method will only close the window that is currently in focus. This method is part of the WebDriver interface and is essential for managing browser sessions and windows during automated testing.




  1. Closing the current browser window:
// Assuming 'driver' is an instance of WebDriver 


import org.openqa.selenium.WebDriver;

public class CloseBrowserExample {
    public static void main(String[] args) {
        // Set path to the ChromeDriver executable
        System.setProperty("", "path/to/chromedriver");

        // Initialize ChromeDriver
        WebDriver driver = new ChromeDriver();

        // Open a webpage
        System.out.println("Page Title: " + driver.getTitle());

        // Close the current browser window


  • Resource Management: The close() method helps in managing system resources by closing unnecessary browser windows during testing.
  • Window Management: It is useful for handling scenarios where multiple windows or tabs are opened, allowing the test to close specific windows while keeping others open.
  • Test Cleanup: Essential for cleaning up after a test case to ensure no browser windows are left open, which could interfere with subsequent tests.

Differences from quit()

  • close(): Closes the current browser window. If there are multiple windows open, only the window with focus will be closed.
  • quit(): Closes all browser windows and ends the WebDriver session. It releases the resources used by the WebDriver.


  • Multiple Windows: If there are multiple windows or tabs open and you need to close all of them, close() will not suffice. You would need to use quit() to close all windows.
  • Session Persistence: The WebDriver session remains active after close(). To completely end the session, use quit().


The close() method in Selenium WebDriver is a vital tool for managing browser windows during automated testing. It allows testers to close the current window, which helps in efficient resource management and ensures that unnecessary windows do not interfere with subsequent tests. Understanding the distinction between close() and quit() is crucial for effective browser session management.

Was this article helpful?
Leave a Reply 0

Your email address will not be published. Required fields are marked *