How To Close Browser Window In Selenium Webdriver

.
Question : How To Close Browser Window In Selenium Webdriver Using Java ?

Answer:
There are to way to close the browser window in selenium webdriver 
  • Close command
  • quit command 
1) Close() 
Close is a selenium webdriver command used to close the current browser window.
During the automation if there are more than one browser windows opened and you want to close only the current browser windows which is having the focus then use the close method . By using Close method you can close current browser window and remaining window will not be closed .Check the below Code to Close the Current Browser Windows.

Syntax 
The Basis Syntax of close method is given below 

close();

Example
The real time example of close method in selenium webdriver is given below

driver.close();

Selenium Code to close the current browser window using close() method .


package Automation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class CloseMethod 
{
public static void main(String[] args)
 {     
System.setProperty("webdriver.chrome.driver","D:\\abc\\Soft\\chromedriver.exe");
 WebDriver drivernew ChromeDriver();
 driver.navigate().to("https://google.com/");
 driver.manage().window().maximize();
 driver.close();
 }
 }
  • The code will Close the Current open browser window means if you are working on multiple browser window then it will close only the current open browser window.

2) quit()
quit is also selenium webdriver command used to close all the browser windows and also terminate all the session.In selenium webdriver we use quit () to close the session properly and clear off the memory . If we do not use quite at the end of program then this may result in memory leak issues.
quit() method will work same as close () method if there is only one browser window open during automation , both method functionality will differ when there is multiple browser window open. 

Syntax
The Basic syntax of quit method given below.

quit();

Example 
Example of quit method given below. 
driver.quit();

Selenium Code to close all browser window using quit() method .


package Automation;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class CloseMethod 
{
public static void main(String[] args)
 {     
System.setProperty("webdriver.chrome.driver","D:\\abc\\Soft\\chromedriver.exe");
 WebDriver drivernew ChromeDriver();
 driver.navigate().to("https://google.com/");
 driver.manage().window().maximize();
 driver.quit();
 }
 }
What is the difference between the close() and quit() in selenium ?
The major difference between close() and quit() is given below.

  • close() is used to close the current browser and quit () is used to close all the opened browser.

In this tutorial we learn how to close Browser window in selenium webdriver .Hope this tutorial helpful for you ðŸ˜Š

Thanks & Regards 
Sandeep Yadav

Related post :
  • How to refresh the web page in selenium webdriver using java
  • how to Click on browser back button in Selenium Webdriver using java
  • How to Click on browser Forward button in Selenium Webdriver using java
  • How to Maximize the window in Selenium Webdriver using java
Important Tags :
how to close the browser window in selenium webdriver , how to close all the browser window in selenium webdriver , how to close browser window in selenium using java . Close() and quit() in selenium


How To Close Browser Window In Selenium Webdriver 


Post a Comment

0 Comments