How to close the whole browser window by keeping the webDriver active?

SeleniumAutomation TestingTesting Tools

We can close the whole browser by keeping the webdriver active with the help of Selenium webdriver. To achieve this we have to use the close method. If there are multiple browsers open the close method shall only close the browser which is in focus but the webdriver session still remains alive.

There is another method called quit. It closes all the opened browsers and terminates the browser session. At the end of the test execution, it is always a good practice to use the quit method to terminate the session properly and avoid memory leaks.

If there is only one browser window opened, the close method can be used to terminate the driver sessions and release all the resources.

Example

Code Implementation with close().

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", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://secure.indeed.com/account/login");
      //implicit wait
      driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
      driver.findElement(By.id("login-google-button")).click();
      // window handles
      Set w = driver.getWindowHandles();
      // window handles iterate
      Iterator t = w.iterator();
      String ch = t.next();
      String pw = t.next();
      // switching child window
      driver.switchTo().window(ch);
      // close only the child browser window
      driver.close();
   }
}

Code Implementation with quit().

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 QuitWindow {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://secure.indeed.com/account/login");
      //implicit wait
      driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
      driver.findElement(By.id("login-google-button")).click();
      // window handles
      Set w = driver.getWindowHandles();
      // window handles iterate
      Iterator t = w.iterator();
      String ch = t.next();
      String pw = t.next();
      // switching child window
      driver.switchTo().window(ch);
      // switching parent window
      driver.switchTo().window(pw);
      // terminates driver session and closes all windows
      driver.quit();
   }
 }
raja
Published on 26-Oct-2020 06:47:04
Advertisements