Selenium:在不关闭浏览器的情况下退出Python脚本

Win*_*ags 5 python selenium sys keyboardinterrupt

我使用以下代码处理使用Ctrl+ C终止正在运行的Python脚本的情况。

except KeyboardInterrupt:
    print "ABORTED"
Run Code Online (Sandbox Code Playgroud)

但是,这也会终止我的Selenium WebDriver浏览器。

有没有办法终止脚本并使浏览器保持活动状态,以便我可以继续使用它?

我通常要做的是通过Ctrl+ 暂停脚本Z。不幸的是,这经常导致浏览器死机并且不响应。

And*_*son 5

您可以将 CTRL+C+ 替换sys.exit()quit()终止Python脚本而不关闭浏览器会话的方法。只需使用以下表格:

user_choice = raw_input('Please click ENTER button to close application')
if not user_choice:
    print "ABORTED"
    quit()
Run Code Online (Sandbox Code Playgroud)