Ever wondered why your browser stays open after tests finish? The answer lies in how you close it!
Closing browser (close vs quit) in Selenium Python - When to Use Which
Imagine you are testing a website manually and have multiple browser windows open. You try to close one window by clicking the 'X', but the others stay open. You have to remember to close each window separately, which is tiring and easy to forget.
Manually closing browser windows one by one is slow and error-prone. You might leave some windows open, wasting memory and causing confusion. It's hard to keep track of which windows are still running, especially during long tests.
Using Selenium's close() and quit() commands lets you control browser windows precisely. close() shuts the current window, while quit() ends the whole browser session, closing all windows. This saves time and avoids mistakes.
driver.close() # Only closes current window, others stay open # Manually closing all windows is tedious
driver.quit()
# Closes all windows and ends session cleanlyIt enables clean, reliable test runs by properly closing browser windows and freeing resources automatically.
When running automated tests overnight, using quit() ensures no browser windows remain open, preventing memory leaks and keeping the test environment fresh for the next run.
Manual closing of browser windows is slow and error-prone.
close() closes one window; quit() closes all and ends the session.
Using these commands keeps tests clean and efficient.