When you finish the script with "Ctrl + C" (KeyboardInterrupt) it does not close Selenium or Firefox

8

I'm using selenium to handle a page, while running the script it opens firefox, but what I wanted to do is the following, if any problem occurs in the script or if the user terminates, I want the firefox to close as well.

I tried several things like:

Use exit:

def __exit__(self):
    self.fechaAba()

Ou or the:

def __del__(self):
    self.fechaAba()

The function I am using is this, it is working, I use it in exceptions to close firefox when the error occurs.

def fechaAba(self):
    try:
        self.__driver.driver.close()
    except:
        setLog("Firefox ja finalizado")

But for example if I press Ctrl + C to finish the script it does not close firefox, the closest I got was using atexit:

import atexit

    def __init__(self):
        atexit.register(self.exit_handler)

    def exit_handler(self):
        self.fechaAba()

It displays the message "Firefox is already finished", but firefox is still open:

KeyboardInterrupt

[2017-09-28 15:16:41]: Firefox ja finalizado

[2017-09-28 15:16:42]: Firefox ja finalizado

When I use the driver.close () it enters the exception and displays the message "Firefox is already finalized", ie it could not execute the command, and when I use quit the effect is similar, it normally executes quit (It does not go into the exception), but firefox remains open.

I gave a small example:

from selenium import webdriver
import time
import atexit

class WhatsappAPI(object):

    driver = None

    def __init__(self):
        print "__init__"
        atexit.register(self.exit_handler)
        self.driver = webdriver.Firefox()
        self.driver.get("http://web.whatsapp.com")

    def comeca_acao(self):
        while True:
            time.sleep(10)
            print "ok"

    def exit_handler(self):
        print "exit_handler"
        self.driver.quit()

    def __exit__(self):
        print "__exit__"
        self.driver.quit()

    def __del__(self):
        print "__del__"
        self.driver.quit()


w = WhatsappAPI()
w.comeca_acao()

Result:

teste.py
__init__
Traceback (most recent call last):
  File "C:\Users\Futurotec\Documents\futurofone_chat\branch\v3.12\_externo\WhatsappBot - Instalador\Final\WhatsappWebAPI\teste.py", line 34, in <module>
    w.comeca_acao()
  File "C:\Users\Futurotec\Documents\futurofone_chat\branch\v3.12\_externo\WhatsappBot - Instalador\Final\WhatsappWebAPI\teste.py", line 17, in comeca_acao
    time.sleep(10)
KeyboardInterrupt
exit_handler
__del__

When it appears in terminal " __init__ " it opens firefox, when I press ctrl c and firefox is still open, what I want is for firefox to close ctrl c or when it enters any exception. In the exceptions already works, but when I press ctrl c no.

    
asked by anonymous 28.09.2017 / 18:27

1 answer

0

You can kill firefox regardless of Selenium, with the result that you kill the process completely, not just the flip that your script started. In addition to not closing the process in a "clean" way.

import os
os.system("taskkill /f /im firefox.exe")

/ f forces shut down. / im makes the taskkill identify the program by name.

    
27.03.2018 / 01:45