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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...