- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to close the whole browser window by keeping the webDriver active?
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(); } }
- Related Articles
- How to close child browser window in Selenium WebDriver using Java?
- How to Resize Browser Window in WebDriver?
- How to close active/current tab without closing the browser in Selenium-Python?
- How to maximize the browser window in Selenium WebDriver using C#?
- How to stretch elements to fit the whole height of the browser window with CSS?
- How to close a current tab in a browser window using JavaScript?
- How can I close a specific window using Selenium WebDriver with Java?
- How to open browser window in incognito/private mode using python selenium webdriver?
- How to open a new window on a browser using Selenium WebDriver for python?
- How to open a browser window in full screen using Selenium WebDriver with C#?
- How to close a Tkinter window by pressing a Button?
- Function to close the window in Tkinter
- How does selenium webdriver upload files to the browser?
- How to close the pop up window in selenium running?
- How to close only the TopLevel window in Python Tkinter?

Advertisements