python - Selenium 有没有办法阻止 ctrl+c 关闭浏览器窗口
问题描述
在我的程序中,我得到了这样的东西:
driver = webdriver.Chrome(options=Options()) #calling the driver
driver.get(website) #opening the website
try:
while True:
do_something() #Here I do a couple of things
except KeyboardInterrupt:
#When I'm done with the while loop above(the goal is to be able to stop at any point of the loop) I use ctrl+c
pass
#Here the program continues but selenium closes the window even though I didn't call for driver.quit().
虽然它成功停止了 while 循环并且不结束程序本身,但 selenium 关闭了窗口浏览器。有没有办法防止硒关闭窗口?
解决方案
当您完成 while 循环而不是使用ctrl+C时,您可以轻松地中断如下:
from selenium.common.exceptions import NoSuchElementException, TimeoutException, WebDriverException
driver = webdriver.Chrome(options=Options()) #calling the driver
driver.get(website) #opening the website
while True:
try:
do_something() #Here I do a couple of things
except (NoSuchElementException, WebDriverException, TimeoutException):
break
# Here the program continues and selenium doesn't closes the window even
推荐阅读
- angular - 使用 Abstract 在 Typescript 中创建基础组件
- java - Apache Camel 消息交换 - 您是否可以迭代交换以从单个消息生成多个输出文件作为交换
- sql-server - SQL Server 2017 - 使用 Gradle 生成 JOOQ 代码
- python - 在 Django 中检测 HttpResponseRedirect 并获取标头数据?
- azure-log-analytics - Kusto:如何从不参与 SUMMARIZE 的列中获取值?
- nativescript - Nativescript 使用 Android 库
- php - 如何计算两个时间戳之间午夜前后的小时数
- gradle - Gradle 多项目,顶部的 ext {} 失败?
- javascript - Google 表格仅查看对运行应用脚本的用户的保护
- android - FingerprintManager - KeyPermanentlyInvalidatedException 未在三星中使用新指纹触发