Close() and Quit() method in Selenium WebDriver

In the previous post, we have written a simple program. In this post, we will learn about the close() and Quit() method in Selenium Webdriver.

Both the methods are used for closing the browser instances. Let’s understand the difference between them.

1. Close() method:

Close() method closes the browser instance which was opened by the WebDriver. Suppose, during execution Webdriver had opened multiple browser windows. When close() method is called, it will close only the browser instance opened by it.

e.g. We will considering testingpool.com. We need to click on the first link “Selenium Introduction” at selenium tutorial page which will open another window of browser. If we use the close method, it closes the first browser instance opened by it.

Code for using Close() method:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenBrowsers {
	private static WebDriver driver;
	public static void main(String[] args) {
		
		driver = new FirefoxDriver();
		driver.get("https://testingpool.com/tutorials/selenium/");
		driver.findElement(By.xpath("//a[text()='A) Selenium Introduction']")).click();
		driver.close();
	}
}

2. Quit() method: 

It closes all the browser window i.e. destroy the WebDriver instance. Suppose, during execution Webdriver had opened multiple browser windows. When quit() method is called, it will close all the browser window opened by Selenium WebDriver.

Don't Forget Note Means Important Remember ForgettingIt does not close the browsers which are not opened by WebDriver.

We will take the same example as above, and see the difference. This time it will close all the browser windows.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenBrowsers {
	private static WebDriver driver;
	public static void main(String[] args) {
		
		driver = new FirefoxDriver();
		driver.get("https://testingpool.com/tutorials/selenium/");
		driver.findElement(By.xpath("//a[text()='A) Selenium Introduction']")).click();
		driver.quit();
	}
}

Ask Question
Have any question or suggestion for us?Please feel free to post in  Q&A Forum

 

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

4 Responses

  1. San Aryal says:

    I saw your Lean FT YouTube video which was really helpful. I could not find other parts. Any plans to post them?

  2. Thanks for appreciation.. Yes, I am planning to post more videos on that.. keep following..

  3. Dipesh Tongiya says:

    How to handle or bypass Distil Network detection through selenium webDriver.

  1. August 16, 2015

    […] the previous post, we have seen the difference between close() and Quit() method of WebDriver. In this post , we will see different types of wait commands available in […]