Ever wondered why your automated tests leave browsers open and slow your computer down?
Closing browser (close vs quit) in Selenium Java - When to Use Which
Imagine you manually test a website by opening multiple browser windows and tabs to check different features. After testing, you try to close each window one by one by clicking the close button.
This manual closing is slow and easy to forget. Sometimes, windows stay open, consuming memory and causing confusion. You might accidentally close the wrong tab or leave some browsers running, which slows down your computer.
Using Selenium's close() and quit() commands lets you control browser windows precisely. close() shuts the current window, while quit() closes all windows and ends the session cleanly, saving time and avoiding leftover processes.
driver.close(); // closes current window only // but other windows stay open
driver.quit(); // closes all windows and ends session cleanlyIt enables fast, reliable cleanup of browser sessions, preventing resource waste and test interference.
When running automated tests overnight, using quit() ensures all browser windows close after tests finish, so the next day your system is ready and clean for new tests.
Manual closing of browsers is slow and error-prone.
close() closes one window; quit() closes all and ends the session.
Using these commands keeps tests clean and system resources free.
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
