【问题标题】:Selenium is there a way to prevent ctrl+c to close browser windowSelenium 有没有办法阻止 ctrl+c 关闭浏览器窗口
【发布时间】:2021-02-02 17:32:44
【问题描述】:

在我的程序中,我得到了这样的东西:

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 会关闭窗口浏览器。有没有办法防止 selenium 关闭窗口?

【问题讨论】:

  • 如果您没有告诉 Selenium 关闭浏览器,它不会关闭它。我们需要查看更多您的代码。
  • 对不起,我编辑了原始帖子并添加了更多信息。关键是我想在任何时候成功地离开 while 循环。我用 ctrl+c 实现了这一点,但这也关闭了窗口浏览器,我想保持打开状态。
  • 我试过了,但由于某种原因,它甚至没有打开浏览器并立即崩溃。
  • “我想在任何时候成功退出while循环”是什么意思,是有特定条件,还是随机结束

标签: python selenium selenium-webdriver try-catch keyboardinterrupt


【解决方案1】:

当你完成 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

【讨论】:

    猜你喜欢
    • 2020-06-06
    • 1970-01-01
    • 2019-07-08
    • 2017-06-08
    • 2012-07-21
    • 2016-03-04
    • 2016-07-11
    • 2020-05-04
    • 1970-01-01
    相关资源
    最近更新 更多