When working with Selenium WebDriver in Java for browser automation, managing how and when browser windows or sessions are terminated is crucial. Two commonly used methods for this purpose are close()
and quit()
. Although they may seem similar at first glance, they serve distinct purposes and have different effects on the browser session.
In this article, we’ll explore the differences between the close()
and quit()
methods in Selenium Java, along with examples and practical use cases to help you use them effectively.
Comparison: close() vs. quit() Methods
Method | What It Does | Scope | WebDriver Session |
---|---|---|---|
close() | Closes only the current browser window or tab | Window/Tab | Remains active if other windows are open |
quit() | Closes all browser windows and tabs opened in the session | Entire Browser | Ends the session and releases all resources |
Understanding close() Method in Selenium
What it does?
- Closes only the current browser window or tab in focus.
- If multiple tabs/windows are open, the rest remain accessible through WebDriver.
- If it’s the last open window,
close()
will behave likequit()
and terminate the session.
Use Case:
The close()
is useful in multi-window scenarios, such as handling pop-ups, child windows, or tabs that need to be individually closed during a test.
Example:
WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); // Perform some actions... driver.close(); // Closes the current browser window/tab
Understanding quit() Method in Selenium
What it does?
- Closes all browser instances opened by the WebDriver during the test.
- Terminates the entire WebDriver session and frees system resources.
- Any further interaction using the driver object after
quit()
will throw an error.
Use Case:
Use quit()
at the end of a test script or test suite to make sure that all browser windows and driver processes are shut down completely.
Example:
WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); // Perform your testing steps... driver.quit(); // Closes all windows and ends the session
Key Differences
Feature | close() | quit() |
---|---|---|
Closes current tab/window | ✅ | ✅ (if it’s the only window) |
Closes all tabs/windows | ❌ | ✅ |
Ends WebDriver session | ❌ (unless last window is closed) | ✅ |
Further use of driver | Allowed if session still exists | ❌ Any further use throws WebDriverException |
Memory/resource cleanup | Partial (session may still run) | Complete |
Best Practices
- Use
close()
when you want to close only a specific popup or child browser tab but continue working with the main application. - Use
quit()
at the end of your test or teardown phase to properly clean up all browser windows and terminate the WebDriver session. - Avoid using driver after quit(): Once you call
quit()
, the driver is no longer valid. Any further commands will throw aNoSuchSessionException
.
Conclusion
Both close()
and quit()
are essential tools in managing browser sessions during Selenium test automation with Java.
- Use
close()
to shut just the current window or tab—especially when handling multiple popups or child windows. - Use
quit()
when you’re done with testing and want to close everything safely and cleanly.
Following these best practices helps maintain system stability, prevents memory leaks, and ensures your test scripts are reliable and professional.