4

How can I catch CTRL+C (a KeyboardInterrupt) without causing the chromedriver to close. It closes the chromedriver when I run my script.py through cmd.

driver = webdriver.Chrome()
try:
    while True:
        #do stuff with chromedriver
except KeyboardInterrupt:
    print "User pressed CTRL + C"
    #do other stuff with chromedriver

It does catch the KeyboardInterrupt in my script, thus my script continues but the chromedriver also gets it and close itself.

EDIT 1:
The solution here doesn't work when you run the script through CMD or when you freeze the script with Pyinstaller and run it (IOError: [Errno 4] Interrupted function call)

EDIT 2:
I also tried by making the script ignore the Errno 4 (using try and except Exception) but still has the same result (chromedriver closes) so in short, this solution does not help at all.

Programer Beginner
  • 1,377
  • 6
  • 21
  • 47
  • Possible duplicate of [How to process SIGTERM signal gracefully?](https://stackoverflow.com/questions/18499497/how-to-process-sigterm-signal-gracefully) – ddavison Sep 09 '17 at 23:28
  • I have the same exact problem on Windows, the same code works on macOS and Linux. I suspect this is because cmd.exe is sending CTRL+C to chromedriver.exe and not to python.exe. – fedterzi Nov 24 '17 at 11:23

3 Answers3

4

Consider using the webdriver.Remote flavor. This option does not spawn a local version of the webdriver inside the interpreter, which should free you from the SIGINT hassle.

Initiate the webdriver in another shell - (chromedriver for Chrome, geckodriver for Firefox, etc.) Take note of the listening port. I will use the defaults here: 9515 for chromedriver and 4444 for geckodriver.

In your python script:

Chrome:

driver=webdriver.Remote("http://127.0.0.1:9515",desired_capabilities=webdriver.DesiredCapabilities.CHROME)

Firerox:

driver=webdriver.Remote("http://127.0.0.1:4444",desired_capabilities=webdriver.DesiredCapabilities.FIREFOX)
smudi
  • 41
  • 2
  • Which args are you trying to supply? – smudi Nov 20 '18 at 14:12
  • Well, mainly user-data-dir and proxy-server, I'm using socks5 proxy and wants to save environment. But I made a mistake to comment since I'm using ruby, watir and chromedriver. And it's working with caps setting now. Still cannot get options or args working directly but satisfied now. Thanks for asking. – Til Nov 22 '18 at 08:51
  • Nice answer! Note that the Webdriver can be started in a terminal just by typing `geckodriver`. – Yan King Yin Dec 13 '21 at 05:08
1

I just tried @Krmit 's answer, with Selenium 4.8.2, python 3.11, and Geckodriver and it worked fine for what I wanted (to be able to cancel a sleep).

    options=Options()
    # options.profile = FirefoxProfile()
    ps = signal.getsignal(signal.SIGINT)
    signal.signal(signal.SIGINT, signal.SIG_IGN)
    driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()), options=options)
    signal.signal(signal.SIGINT, ps)
Ned Konz
  • 11
  • 2
0

You can also just disable SIGINT handling while starting the driver. Like so:

import signal 

...

ps = signal.getsignal(signal.SIGINT) # backup signal handler 
signal.signal(signal.SIGINT, signal.SIG_IGN) # ignore signal temporarily

... = webdriver.Chrome(...) 

signal.signal(signal.SIGINT, ps) # restore original handler
Krmit
  • 21
  • 2