Tuesday, September 10, 2019

Post#10.Close Browser


Close Browser

To close a browser we need to call close() method from WebDriver, we can also work without closing a browser (i.e) we can open another browser instance without closing existing browser but existing browser takes little space.
package pack2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CloseBrowser
{
              public static void main(String[] args) throws Exception
              {
                             WebDriver driver=new FirefoxDriver();
                             driver.close(); //closes the browser
              }
}
Quit Browser

To close a browser window we need to call close() method from WebDriver, but what if we want to close all browser windows opened by current instance. To Close all windows we need to call quit() method from WebDriver.
package pack2;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class QuitBrowser
{
              public static void main(String[] args) throws Exception
              {
                             WebDriver driver=new FirefoxDriver();
                             driver.quit(); //closes all browser windows
              }
}
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 , quit() also terminates the driverserver from the CPU process which save us some memory but close() will not do it.

Once quit() is used 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 sitchTo().Window() command to get the control of other window.


No comments:

Post a Comment