Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Selenium WebDriver: Understanding the close() Method in Java

Posted on July 21, 2025July 21, 2025 By Admin No Comments on Selenium WebDriver: Understanding the close() Method in Java

The close() method is an essential part of Selenium WebDriver’s API in Java, specifically designed for closing the current browser window or tab that is in focus during test automation. Mastery of this method helps manage browser sessions reliably, especially when dealing with multiple windows or tabs.

What Does close() Do?

  • Closes the Current Window in Focus: When invoked, close() shuts only the browser window or tab currently controlled by WebDriver.
  • Session Remains Active: If multiple windows exist, the WebDriver session continues for remaining open windows or tabs.
  • Single Window Behavior: If close() is called on the final open window or tab, it effectively ends the browser session, similar to the quit() method.

Syntax

driver.close();
  • Here, driver refers to your active WebDriver instance.

Complete Code Example

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

public class CloseExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.example.com");

        // Close the current browser window
        driver.close();
    }
}

Result: Only the browser window currently controlled by WebDriver closes. The WebDriver session persists if other windows remain open.

Working with Multiple Windows

When automating interactions across multiple windows or tabs, close() is used after switching WebDriver’s control to the intended window using switchTo().window(windowHandle):

// Switch to a specific window first
driver.switchTo().window(windowHandle);
driver.close(); // Closes the focused window only

This allows precise control over which window gets closed, leaving others unaffected.

close() vs quit() Methods: Key Differences

Featureclose()quit()
Closes current window/tab onlyYesNo, closes all windows/tabs
Closes all browser windows/tabsNoYes
Ends WebDriver sessionNo (unless last window is closed)Yes
Common useClose popups, child windowsEnd of test suite or script

Typical Use Cases

  • Closing popups or child windows while retaining the main browser session.
  • Sequentially closing test-created windows without terminating the entire browser session.
  • Cleaning up after specific interactions in a multi-window flow.

Best Practices

  • Switch to the correct window before calling close() to avoid unintentional closure.
  • Use quit() for complete cleanup at the end of your test suite, as it releases all browser resources.
  • Be aware that calling close() on the last open window will terminate the WebDriver session.
Selenium Tags:Selenium-Java

Post navigation

Previous Post: Selenium WebDriver: Understanding the navigate() Method in Java
Next Post: Selenium WebDriver: Understanding the quit() Method in Java

Leave a Reply Cancel reply

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

Categories

  • CSS
  • HTML
  • Interview Experience
  • Java
  • JavaScript
  • Lodash
  • PHP
  • Programs
  • Python
  • Selenium
  • Software Testing
  • Web Technologies
  • Web Templates

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme