In this tutorial, we will discuss on Difference between the close() and quit() methods in Selenium WebDriver
‘close()’ method closes the currently active/focus window or tab of the current browser instance.
‘quit()’ method closes all open windows or tabs of the current browser instance.
Example:
package TestAutomationCentral;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class CloseAndQuitDemo {
public static void main(String[] args) throws InterruptedException {
//TestAutomationCentral.com
System.setProperty("webdriver.chrome.driver", "C:\\temp\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.switchTo().newWindow(WindowType.WINDOW);
driver.get("https://testautomationcentral.com/");
Thread.sleep(2500);
driver.close();
Thread.sleep(2500);
driver.quit();
}
}
In the above Selenium WebDriver Java example, we have used Thread.sleep(2500) to validate the close() and quit(). Otherwise, the execution will be very fast and we can’t notice quit() and close()