Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script with Selenium leaves firefox.exe running when manually closing application

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.

zombie processes after manually closing .exe

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?

like image 909
Morello Avatar asked May 22 '26 12:05

Morello


1 Answers

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
like image 139
jizhihaoSAMA Avatar answered May 24 '26 00:05

jizhihaoSAMA