Close() and Quit() method in Selenium WebDriver
In the previous post, we have written a simple program. In this post, we will learn about the close() and Quit() method in Selenium Webdriver.
Both the methods are used for closing the browser instances. Let’s understand the difference between them.
1. Close() method:
Close() method closes the browser instance which was opened by the WebDriver. Suppose, during execution Webdriver had opened multiple browser windows. When close() method is called, it will close only the browser instance opened by it.
e.g. We will considering testingpool.com. We need to click on the first link “Selenium Introduction” at selenium tutorial page which will open another window of browser. If we use the close method, it closes the first browser instance opened by it.
Code for using Close() method:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class OpenBrowsers { private static WebDriver driver; public static void main(String[] args) { driver = new FirefoxDriver(); driver.get("https://testingpool.com/tutorials/selenium/"); driver.findElement(By.xpath("//a[text()='A) Selenium Introduction']")).click(); driver.close(); } }
2. Quit() method:
It closes all the browser window i.e. destroy the WebDriver instance. Suppose, during execution Webdriver had opened multiple browser windows. When quit() method is called, it will close all the browser window opened by Selenium WebDriver.
We will take the same example as above, and see the difference. This time it will close all the browser windows.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class OpenBrowsers { private static WebDriver driver; public static void main(String[] args) { driver = new FirefoxDriver(); driver.get("https://testingpool.com/tutorials/selenium/"); driver.findElement(By.xpath("//a[text()='A) Selenium Introduction']")).click(); driver.quit(); } }
I saw your Lean FT YouTube video which was really helpful. I could not find other parts. Any plans to post them?
Thanks for appreciation.. Yes, I am planning to post more videos on that.. keep following..
How to handle or bypass Distil Network detection through selenium webDriver.