Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 硒有没有一种方法可以防止ctrl+;c关闭浏览器窗口_Python_Selenium_Selenium Webdriver_Try Catch_Keyboardinterrupt - Fatal编程技术网

Python 硒有没有一种方法可以防止ctrl+;c关闭浏览器窗口

Python 硒有没有一种方法可以防止ctrl+;c关闭浏览器窗口,python,selenium,selenium-webdriver,try-catch,keyboardinterrupt,Python,Selenium,Selenium Webdriver,Try Catch,Keyboardinterrupt,在我的程序中,我得到了如下内容: 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 th

在我的程序中,我得到了如下内容:

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().

当selenium成功停止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关闭浏览器,它将不会关闭浏览器。我们需要查看您的更多代码。抱歉,我编辑了原始帖子并添加了更多信息。关键是我希望在任何时候都能成功地离开while循环。我用ctrl+c实现了这一点,但这也会关闭窗口浏览器,我想保持它的打开状态。嗯。。。看,我试过了,但出于某种原因,它甚至没有打开浏览器,然后就崩溃了。在“我想在任何时候成功地离开while循环”下,你有什么具体的条件,还是在随机结束while循环