Skip to content

WebDevHubs

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

Selenium WebDriver in Java: Difference Between close() and quit() Methods

Posted on July 21, 2025July 21, 2025 By Admin No Comments on Selenium WebDriver in Java: Difference Between close() and quit() Methods

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

MethodWhat It DoesScopeWebDriver Session
close()Closes only the current browser window or tabWindow/TabRemains active if other windows are open
quit()Closes all browser windows and tabs opened in the sessionEntire BrowserEnds 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 like quit() 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

Featureclose()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 driverAllowed if session still exists❌ Any further use throws WebDriverException
Memory/resource cleanupPartial (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 a NoSuchSessionException.

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.

Selenium Tags:Selenium-Java

Post navigation

Previous Post: Selenium WebDriver: Understanding the quit() Method in Java
Next Post: Selenium WebDriver: Understanding the sendKeys() 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