Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium: Quit Python script without closing browser

I use the following to handle the situation where Ctrl + C is used to terminate a running Python script.

except KeyboardInterrupt:
    print "ABORTED"

However, this also terminates my Selenium WebDriver browser.

Is there a way to terminate the script and keep the browser alive, so that I can continue using it?

What I usually do instead, is to pause the script via Ctrl + Z. This unfortunately often causes the browser to freeze and not respond.

like image 590
P A N Avatar asked Mar 31 '16 13:03

P A N


People also ask

What is the difference between quit () and close () method in Selenium?

The difference between quit() and close() driver. quit() : The quit() method quits the driver, closing every associated window. driver. close() : The close() method closes the currently focused window, quitting the driver if the current window is the only open window.

How do I keep a session alive in Selenium?

We can keep a session alive for long Selenium scripts in automation. In Chrome browser, this can be achieved with the help of the ChromeOptions and Capabilities classes. Capabilities class can get the capabilities of the browser by using the method – getCapabilities.


1 Answers

You can replace CTRL+C+sys.exit() with quit() method to terminate Python script without closing browser session. Just use following form:

user_choice = raw_input('Please click ENTER button to close application')
if not user_choice:
    print "ABORTED"
    quit()
like image 66
Andersson Avatar answered Sep 29 '22 10:09

Andersson