We close browsers in tests to free up resources and avoid leftover windows. Knowing the difference between close() and quit() helps control browser behavior properly.
Closing browser (close vs quit) in Selenium Java
driver.close(); driver.quit();
close() closes the current browser window or tab.
quit() closes all browser windows and ends the WebDriver session.
driver.close();
driver.quit();
driver.get("https://example.com");
driver.close();driver.get("https://example.com");
driver.quit();This program shows the difference between close() and quit(). After close(), the driver session still exists but the current window is closed. After quit(), the driver session ends and cannot be used.
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class BrowserCloseVsQuit { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("https://example.com"); System.out.println("Title before close: " + driver.getTitle()); driver.close(); try { driver.getTitle(); } catch (Exception e) { System.out.println("After close(), driver session is still alive but current window is closed."); } // Start new driver for quit demo driver = new ChromeDriver(); driver.get("https://example.com"); System.out.println("Title before quit: " + driver.getTitle()); driver.quit(); try { driver.getTitle(); } catch (Exception e) { System.out.println("After quit(), driver session is closed and unusable."); } } }
Use close() when you want to close one window but keep the session alive.
Use quit() to end the session and close all windows to free resources.
Calling close() on the last window may also end the session depending on the browser.
close() closes the current browser window or tab.
quit() closes all browser windows and ends the WebDriver session.
Choose the method based on whether you want to keep the session alive or end it completely.
Practice
close() method do in Selenium WebDriver?Solution
Step 1: Understand the purpose of
Theclose()close()method is designed to close only the current browser window or tab that the WebDriver is controlling.Step 2: Differentiate from
Unlikequit()quit(), which closes all windows and ends the session,close()affects only one window or tab.Final Answer:
Closes the current browser window or tab only -> Option AQuick Check:
close()closes one window [OK]
- Confusing close() with quit()
- Thinking close() ends the WebDriver session
- Assuming close() clears cookies
Solution
Step 1: Recall the method to end WebDriver session
The methodquit()is used to close all browser windows and end the WebDriver session.Step 2: Check syntax correctness
The correct syntax in Java Selenium isdriver.quit();. Other options likeexit()orshutdown()do not exist in WebDriver API.Final Answer:
driver.quit(); -> Option BQuick Check:
Use quit() to close all windows [OK]
- Using close() instead of quit() to end session
- Using non-existent methods like exit() or shutdown()
- Missing parentheses in method call
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
driver.close();
driver.get("https://google.com");What will happen when this code runs?
Solution
Step 1: Analyze the effect of driver.close()
Theclose()method closes the current browser window or tab but does not end the WebDriver session. However, if it is the only window, the session becomes invalid.Step 2: Understand the next get() call after close()
After closing the only window, callingdriver.get()will cause aNoSuchWindowExceptionor similar because there is no open window to navigate.Final Answer:
The browser opens example.com, closes the tab, then throws an exception on the next get() call -> Option DQuick Check:
close() closes window; next get() fails if no window [OK]
- Assuming get() opens new window after close()
- Thinking close() ends session like quit()
- Ignoring exceptions after closing last window
driver.close(); driver.quit();
What is the problem with this code?
Solution
Step 1: Understand the effect of close()
Callingclose()closes the current window. If it is the last window, no windows remain open, but the WebDriver session remains active.Step 2: Analyze quit() after close()
Callingquit()afterclose()safely terminates the session even with no windows left; no exception is thrown.Final Answer:
No problem; calling close() before quit() works without throwing exceptions -> Option AQuick Check:
close() then quit() succeeds [OK]
- Thinking close() before quit() throws exception
- Confusing close() and quit() effects
- Believing quit() must precede close()
Solution
Step 1: Identify the requirement to keep session alive
To keep the WebDriver session active, you must not callquit()because it ends the session and closes all windows.Step 2: Choose method that closes only current tab
Theclose()method closes only the current tab or window but keeps the WebDriver session alive, allowing further commands like opening new tabs.Final Answer:
Use driver.close() because it closes only the current tab and keeps session alive -> Option CQuick Check:
close() closes tab; quit() ends session [OK]
- Using quit() and losing session
- Calling quit() then expecting to open new tabs
- Confusing close() with quit() effects
Ready to prove it?
Pass the test and this pattern moves unknown
