Close Browser in selenium

To close a browser, we need to call a close() method from webdriver; we can also work without closing a browser (i.e.) we can open another browser instance without closing the existing browser, but existing the browser takes little space.

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

public class CloseBrowser
{
	public static void main(String[] args) throws Exception
	{
		System.setProperty("webdriver.gecko.driver", "D:PATHgeckodriver.exe");
		WebDriver driver=new FirefoxDriver();
		driver.close(); //closes the browser
	}
}

Open browser in Selenium

Quit Browser in selenium

To close a browser window, we need to call the close() method from the webdriver, but what if we want to close all browser windows opened by the current instance. To Close all windows we need to call quit() method from webdriver

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

public class QuitBrowser
{
	public static void main(String[] args) throws Exception
	{
		System.setProperty("webdriver.gecko.driver", "D:PATHgeckodriver.exe");
		WebDriver driver=new FirefoxDriver();
		driver.quit(); //closes all browser windows
	}
}

Selenium Data Provider

Quit Browser vs. Close Browser

Close browser closes the current browser window, Quit browser will close the all the browser windows opened by the driver

Quit browser will not only just closes the browser windows, but quit() also terminates the driverserver from the CPU process, which saves us some memory but close() will not do it.

Once quit() is used, then the driver object becomes not-reachable, so you cannot perform any operation on the driver object. Close() browser make the driver object still avail to the user.

how to close a tab in selenium webdriver:

You can close the current tab using the close() method in selenium but after closing the tab make sure you are using switchTo().Window() command to get the control of other windows.

how to the close already opened the browser in selenium:

First and foremost is this, you cannot connect to the existing browser window in selenium. So closing an already opened browser in selenium is not possible.

Open Webpage in Selenium

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions