Difference Between Close and Quit Method in Selenium Web Driver

In This Section, we are Going to Learn the Difference Between driver.Close(); and  driver.quit(); Method in Selenium

Difference between Close() and Quit () methods

Close() method is used to close the active window of the browser and Quite() method is used to close the complete browser, Including Inactive windows. Technical the Selenium driver object is closed at the time of Quit().

Close() in Selenium

So when we call the close method, in that case, it is going to close the browser window which is currently pointed by the driver property, which means it will close the last browser window but still, the other two browser windows will remain active.

Quit() in Selenium

Quit() it is used to close all the Browser windows it doesn’t matter if they’re single or multiple it will close all of them and also stop the WebDriver. So that’s all about the Page Navigation and Close and Quit methods in Selenium.

Real-time use of Close and Quit Method in Selenium

To Demonstrate the Difference Between the close(); and quit(); methods we are going to use two web drivers one chrome and the other one  Edge, in which we are going to assign a driver.close();method to chrome Web driver  and driver.quit();To Edge Web Driver

Test Scenario:
  • Open the Chrome and Edge Browsers.
  • Load the Url “https://www.google.com”
  • close Chrome Web Driver with driver.close();
  • close Edge Web Driver with driver.quit();
  • and check background Process for Chome and Edge web Drivers

Create Selenium Script in Eclipse IDE: –
• Open Eclipse IDE and click on file >> new >> class.
• Enter Class Name “close_quit” and Click Finish.

Code:-
package project1;

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

public class close_quit {
    public static void main(String[]args)// Main Java Class
{
        
//Chrome WebDriver Setup
System.setProperty("webdriver.chrome.driver", "E:/CodeBun/project1/chromedriver.exe");//initialize Chrome Web Driver
WebDriver driverChrome = new ChromeDriver();// Create Instance for Chrome Web Driver 
driverChrome.manage().window().maximize();// Maximize Window for Chrome Instance 
driverChrome.get("https://www.google.com");//Url Request Form Chrome Driver
driverChrome.close();// This Would Close the Current focus window

//Edge WebDriver Setup
System.setProperty("webdriver.edge.driver", "E:/CodeBun/Project3/msedgedriver.exe");//initialize Edge Web Driver
WebDriver driverEdge = new EdgeDriver();//Create Instance for Edge Web Driver
driverEdge.manage().window().maximize();// Maximize Window for Edge Instance
driverEdge.get("https://www.google.com");//Url Request Form Edge Driver
driverEdge.quit();// this would close all the window of web driver
}
}

Code Explanation:

Close() using Chrome Driver

System.setProperty("webdriver.chrome.driver","E:/CodeBun/project1/chromedriver.exe");

In the Above Example,System.setproperty()the method is Used to Initialize with Chrome Web driver.

WebDriver driverChrome = new ChromeDriver();

WebDriver driver = new ChromeDriver()is Used To Create an instance of Chrome.

driverChrome.manage().window().maximize();

driverChrome.manage().window().maximize()is used for maximizing the chrome instance window.

driverChrome.get("https://www.google.com");

driver.get(“URL”)is used for Requesting Specific URL, in the Above Statement, it is Requested by Chrome Driver.

driverChrome.close();

driverChrome.close()this would only close the current window but the driver will be active in the background.

Quite() using Edge Web Driver in Java Selenium

System.setProperty("webdriver.edge.driver","E:/CodeBun/Project3/msedgedriver.exe");

In the Above Example,System.setproperty()the method is Used to Initialize with Edge Web driver.

WebDriver driverEdge = new EdgeDriver();

WebDriver driverEdge = new EdgeDriver()is Used To Create an instance of Edge.

driverEdge.manage().window().maximize();

driverEdge.manage().window().maximize()is used for maximizing the Edge instance window.

driverEdge.get("https://www.google.com");

driverEdge.get("https://www.google.com")is used for Requesting Specific URL, in the Above Statement, it is Requested by Edge Driver.

driverEdge.quit();this would close all windows Open by Edge Web Driver Also driver Will be Closed background.

Output:-

As You can see SnapShot Above before Executing the Program None of the Web Drivers are Running

As you Can See Snapshot Above After Running the Program both Web Driver Got Executed But the Chrome Web Driver Kept Running In the Background Because  driver.close()the method was used  Whereas in the case of Edge Web Driver driver.quit()was Used which closed the Driver Instance.

You can check Background Process from Command Prompt or IN Task Manager Under Details Tab

To Check Process from Command Prompt enter Below commands

For Chrome Web Driver:-

tasklist /fi "imagename eq chromedriver.exe"

For Edge Web Driver:-

tasklist /fi "imagename eq msedgedriver.exe"

We Have Successfully Completed Demonstrated the Difference Between the Close() and quit() Method.