Closing browser (close vs quit) in Selenium Java - Automation Approaches Compared
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.NoSuchSessionException; public class BrowserCloseVsQuitTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); // Test driver.close() WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com"); // Close current window driver.close(); // After close(), try to get current URL to check if session is alive try { String url = driver.getCurrentUrl(); System.out.println("Session alive after close(), current URL: " + url); } catch (NoSuchSessionException e) { System.out.println("Session ended after close(): " + e.getMessage()); } } catch (Exception e) { e.printStackTrace(); } finally { // Quit driver to clean up if session still alive try { driver.quit(); } catch (Exception ignored) {} } // Test driver.quit() WebDriver driver2 = new ChromeDriver(); try { driver2.get("https://example.com"); // Quit closes all windows and ends session driver2.quit(); // After quit(), try to get current URL to verify session ended try { String url = driver2.getCurrentUrl(); System.out.println("Session alive after quit(), current URL: " + url); } catch (NoSuchSessionException e) { System.out.println("Session ended after quit(): " + e.getMessage()); } } catch (Exception e) { e.printStackTrace(); } } }
This code demonstrates the difference between driver.close() and driver.quit() in Selenium WebDriver using Java.
First, it opens a browser and navigates to example.com, then calls driver.close() which closes the current browser window. We then try to get the current URL to check if the WebDriver session is still alive. Usually, if only one window was open, the session may still be alive but the window is closed.
Next, it opens a new browser session and navigates to example.com again, then calls driver.quit() which closes all browser windows and ends the WebDriver session. Trying to get the current URL after quit throws a NoSuchSessionException, confirming the session ended.
Using try-finally blocks ensures the driver quits properly to avoid leftover browser processes. Catching exceptions helps verify session state after close and quit.
Now add data-driven testing to open multiple URLs and test close vs quit behavior for each.
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
