7O07Y7
7O07Y7

Reputation: 569

Bind Key to Stop Script

I have a URL in a while loop which repeats the process. Is it possible to bind a key to stop a selenium python script from running while still keeping chromedriver open?

Upvotes: 0

Views: 260

Answers (1)

sytech
sytech

Reputation: 41119

You can use a try/except block that catches a KeyboardInterrupt exception (IE when you input ctrl + c to the terminal/command prompt)

try:
    while True:
        #dostuff
except KeyboardInterrupt:
    print("Loop stopped!")

You may also want to consider launching your script in interactive mode, which will prevent the console from closing and will keep the interpreter open after the completion (or failure) of the script, thereby preventing the teardown/cleanup that closes the webdriver instance.

Upvotes: 1

Related Questions