My Python script uses Selenium to automate a browser on Windows. I want to be able to interrupt the script by pressing CTRL+C. But even if I catch the exception, the WebDriver terminates on the interrupt:
from selenium import webdriverdriver = webdriver.Chrome()try: while True: passexcept KeyboardInterrupt: print('KeyboardInterrupt caught')driver.get('https://www.google.com') # will failWhen CTRL+C is pressed, the WebDriver window closes immediately. Any subsequent calls to driver methods raise a urllib3 exception.
If I am using WebDriver in Python REPL and I need to terminate some function via interrupt, I don't want that to destroy my driver instance. Similarly when testing a longer automated process, I would like to be able to cancel execution manually if something goes awry but allow the browser window to remain open for debugging.
I could use an instance of Remote WebDriver (via Selenium Grid) on my local machine as suggested here but I do not want to add complexity with managing the grid. The solutions mentioned here seem to only work on Unix.
Is there a simple way to achieve this on Windows?





