硒有一种方法来防止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

最新更新