Bind Key to Stop Script
<p>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?</p>

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

<pre><code>try:
    while True:
        #dostuff
except KeyboardInterrupt:
    print("Loop stopped!")
</code></pre>

<p>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.</p>

python selenium webdriver selenium-chromedriver