Difference between quit() and close()

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

The main difference between the quit() and close() methods in Selenium is their impact on the browser instance. The quit() method closes the entire browser and terminates the WebDriver session. It closes all open browser windows and releases the associated memory and resources. On the other hand, the close() method is used to close the current browser window or tab while keeping the WebDriver session active. It is useful when dealing with multiple browser windows or tabs, allowing you to close a specific window without terminating the entire browser session.

How to Close a Browser in Selenium?

To close a browser in Selenium, you can use the close() or quit() methods depending on your specific requirements.

  1. To close the current browser window: driver.close();
  2. Quit the entire browser and terminate the WebDriver session: driver.quit();

The close() method will close the current browser window or tab while keeping the WebDriver session active. However, if it is the only window or tab open, it will behave similarly to the quit() method and terminate the entire browser and session.

Comparing close() and quit():

Close():

  • Closes the active tab or window of the browser.
  • Keeps the WebDriver session active if other windows or tabs remain open.
  • Terminates the entire browser and session if it is the last window or tab.

Quit():

  • Closes all open browser windows or tabs.
  • Terminates the entire browser and session, releasing associated memory and resources.
  • Recommended to use when ending the WebDriver session.

In summary, close() is used to close a specific window or tab, while quit() is used to close all windows or tabs and terminate the WebDriver session. The appropriate method depends on whether you want to close a specific window or terminate the entire browser session.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

driver.close()

The driver.close() method in Selenium closes the current browser window or tab while keeping the WebDriver session active. It is particularly useful when dealing with multiple browser windows or tabs, and you want to close a specific window without terminating the entire browser session.

Let's look into an example of how to use driver.close():

Code Explanation

Let's understand the above code example.

In the above example, we first set the path to the ChromeDriver executable using System.setProperty(). Then, we create an instance of the ChromeDriver class to initialize the Chrome browser. Next, we navigate a website using driver.get(). After performing the necessary actions in the browser window, we call driver.close() to close the current browser window.

Ensure that path/to/chromedriver refers to the correct location of the ChromeDriver executable file on your system.

By using driver.close(), you can close the current browser window or tab while keeping the WebDriver session active in your Java Selenium code.

It's important to note that if driver.close() is called on the last remaining window or tab, it will behave similarly to the driver.quit() method and terminate the entire browser session.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

driver.quit()

The driver.quit() method in Selenium closes all open browser windows or tabs and terminates the WebDriver session. It is typically used at the end of a test script to ensure all browser instances are closed properly, and any associated resources are released.

Let's look into an example of how to use driver.quit():

Code Explanation

Let's understand the above code example.

In the above example, we first set the path to the ChromeDriver executable using System.setProperty(). Then, we create an instance of the ChromeDriver class to initialize the Chrome browser. Next, we navigate a website using driver.get(). After performing the necessary actions in the browser window, we call driver.quit() to close all browser windows or tabs and terminate the WebDriver session.

Ensure that "path/to/chromedriver" refers to the correct location of the ChromeDriver executable file on your system.

By using driver.quit(), you ensure that all browser instances are closed and resources are properly released in your Java Selenium code.

Conclusion

  • The main difference between the quit() and close() methods in Selenium is their impact on the browser instance. Thequit() is used to close the entire browser and terminate the WebDriver session, while close() is used to close the current browser window or tab while keeping the WebDriver session active.
  • close() is useful when dealing with multiple browser windows or tabs, allowing you to close a specific window without terminating the entire session.
  • If close() is called on the last remaining window or tab, it will behave similarly to quit() and terminate the entire browser session.
  • quit() closes all open browser windows or tabs and releases associated memory and resources.
  • It is recommended to use quit() when ending the WebDriver session, ensuring proper closure of all browser instances.
  • close() is suitable for closing specific windows or tabs while keeping the WebDriver session active.