Learn to Close Browser in Selenium

Test your website on different browsers using Selenium Browser Automation. Know how to close a browser in automated Selenium tests.

Get Started free
Home Guide How to Close a Browser in Selenium

How to Close a Browser in Selenium

By Neha Vaidya, Community Contributor -

Selenium is used for automation testing of web applications. In this case, web applications refer to software that makes it necessary to instantiate a browser instance, open a browser and then close a browser when the testing is complete. During testing cycles, the entire process of browser automation is done by Selenium WebDriver.

This article will focus on how to close a browser in Selenium.

What is Selenium?

Selenium is an open-source tool that automates web browsers to replicate user actions for QA purposes. It provides a single interface that lets testers write test scripts in programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C#, among others.

To explore the basics of Selenium and its usage in testing, refer to this page.

Before proceeding with this, it is important to know how to execute your first test case in Selenium to understand the flow and operation.

How to Close a Browser in Selenium

To close a browser using Selenium, simply use the close() method. Refer to the code below as an example:

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

public class CloseBrowserExample{
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "Path_to_Chrome_Driver");
WebDriver driver=new ChromeDriver();
driver.close(); //closes the browser
}
}

Now let’s understand a bit more about driver.close and driver.quit methods which are used to close a browser in Selenium.

driver.close() and driver.quit() are two methods for closing a browser session in Selenium WebDriver. It is necessary to know when to use each method in a test script.

  • driver.close() closes only the current window on which Selenium is running automated tests. The WebDriver session, however, remains active.
  • On the other hand, the driver.quit() method closes all browser windows and ends the WebDriver session.

Note that if no other window is open and the script calls driver.close() then it will also close the current WebDriver session.

BrowserStack Automate Banner 7

Here arises the question: when should a tester use which method?

Let’s take an example to understand how to call both these methods in Selenium.

driver.close()

This method is used when you want to close the current existing window you are working on. Please note driver.close() will not terminate the browser instance.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
public class CloseWindow {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "Path to Chrome Driver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
//implicit wait
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.findElement(By.name(“q")).sendkeys(Selenium);
driver.findElement(By.name(“btnk")).click();
// window handles
Set w = driver.getWindowHandles();
// window handles iterate
Iterator t = w.iterator();
String h = (String) t.next();
String p = (String) t.next();
// switching child window
driver.switchTo().window(h);
// close only the child browser window
driver.close();
}
}’

On executing the above code, output goes as follows:

Talk to an Expert

driver.quit()

This method is used when you want to terminate the webdriver instance window you are working on. Please note driver.quit() will terminate the browser instance.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
public class CloseWindow {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "Path to Chrome Driver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
//implicit wait
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.findElement(By.name(“q")).sendkeys(Selenium);
driver.findElement(By.name(“btnk")).click();
// window handles
Set w = driver.getWindowHandles();
// window handles iterate
Iterator t = w.iterator();
String h = t.next();
String p = t.next();
// switching child window
driver.switchTo().window(h);
// switching parent window
driver.switchTo().window(p);
// terminates driver session and closes all windows
driver.quit();
}
}

Bear in mind that Selenium tests must be run on real devices for accurate results. Start running tests on 3000+ real browsers and devices on BrowserStack’s real device cloud. Run parallel tests on a Cloud Selenium Grid to get faster results without compromising on accuracy. Detect bugs before users do by testing software in real user conditions with BrowserStack.

Try BrowserStack for Free

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

How to perform Mouse Hover Action in Selenium

How to handle Multiple Tabs in Selenium

Automation Tests on Real Devices & Browsers

Seamlessly Run Automation Tests on 3500+ real Devices & Browsers