I have a simple script that runs on Selenium. I made an .exe file out of it and it works perfectly fine but there is one problem I have noticed. The .exe opens a console as intended but if I manually close the console while the script is running, it leaves firefox.exe opened in Processes tab.

My question is: how do I make Python close these processes in case if the script's execution has been interrupted manually?
I'm guessing I need to catch the exception, but what exception does Python throw in case if we manually stop the program?
You could use atexit module to quit the webdriver automatically before the script stopped.
import atexit
from selenium import webdriver
def exit_handler(): # when exit, execute this function
driver.quit()
atexit.register(exit_handler)
driver = webdriver.Firefox()
# Your work
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With