简体   繁体   English

driver.quit() 在未处理的异常之后

[英]driver.quit() after unhandled exception

I need some help with this issue that I'm having.我需要一些帮助来解决我遇到的这个问题。

I have a python script that I'm using to keep another python script running on a loop.我有一个 python 脚本,我用它来保持另一个 python 脚本在循环上运行。

I basically call it like this python loop.py script.py我基本上这样称呼它python loop.py script.py

The script.py uses selenium and chromedriver to perform whatever it needs to do, I'm trying to keep this script running 24/7, so whenever it fails, the loop script just restarts it and opens another instance of the chromedriver, opening another chrome window. script.py使用 selenium 和 chromedriver 来执行它需要做的任何事情,我试图让这个脚本 24/7 运行,所以每当它失败时,循环脚本就会重新启动它并打开 chromedriver 的另一个实例,打开另一个铬 window。

My problem is that whenever I keep this script running for several hours, there's a point where I have too many chrome windows opened, causing the computer to go crazy slow as you would expect.我的问题是,每当我让这个脚本运行几个小时时,都会有一个点,我打开了太多的 chrome windows,导致计算机像你期望的那样疯狂地慢到 go。

script.py is divided into multiple functions, with one function calling some of the other functions like: script.py分为多个函数,其中一个 function 调用其他一些函数,例如:

def caller_function():
  function1()
  function2()
  function3()
  function4()

I already have some of the exceptions handled so basically the scripts only stops whenever an unhandled exception occurs.我已经处理了一些异常,所以基本上脚本只会在发生未处理的异常时停止。

This unhandled exception can happen in any of the functions so I can't seem to find a way around handling(?) the unhandled exception to make it do a driver.quit() so it closes the instance that failed before the loop.py calls script.py again and opens another chrome window.这个未处理的异常可能发生在任何函数中,所以我似乎无法找到一种方法来处理(?)未处理的异常以使其执行driver.quit()所以它关闭在loop.py之前失败的实例再次调用script.py并打开另一个 chrome window。

I tried doing:我试着做:

def caller_function():
 try:
   function1()
   function2()
   function3()
   function4()
 except:
   driver.quit()

But it didn't work, as for some reason the driver.quit() fell into a loop and stopped loop.py from restarting script.py但它不起作用,因为某种原因 driver.quit() 陷入循环并停止loop.py重新启动script.py

Basically, I'm trying not to have 20+ chrome windows open whenever I wake up in the morning to check the progress of the script.基本上,每当我早上醒来检查脚本的进度时,我都尽量不打开 20+ chrome windows。 Does anyone know a way around this?有谁知道解决这个问题的方法?

use finally for that purpose: finally 用于此目的:

 try:
   # your code

 finally:
   driver.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 driver.quit 后调用 chromedriver 不起作用? - Invoking chromedriver after driver.quit not working? Selenium ChromeDriver-driver.quit()上的HTTP 407 - Selenium ChromeDriver - HTTP 407 on driver.quit() 在所有情况下调用 driver.quit() - invoke driver.quit() in all cases python selenium driver.quit() 里面除了块 - python selenium driver.quit() inside except block python selenium driver.quit() 不会中途终止程序 - python selenium driver.quit() won't terminate the program midway 如果设置了firefox_profile,则python selenium driver.quit()无法退出firefox浏览器 - python selenium driver.quit() can not quit firefox browser if firefox_profile setted Selenium 中的 driver.quit():ConnectionRefusedError 如果用于多个驱动程序 object - driver.quit() in Selenium : ConnectionRefusedError if used on more than one driver object 尽管 driver.close 和 driver.quit,IDLE 不会终止我的 selenium 浏览器 - IDLE doesn't terminate my selenium broswer despite driver.close and driver.quit driver.quit() 和 pkill chrome 释放内存的效果一样吗? - Do driver.quit() and pkill chrome have the same effect in releasing memory? 硒3.0.2中的driver.quit firefox 50.1.0 firefox已停止工作 - driver.quit in selenium 3.0.2 firefox 50.1.0 firefox has stopped working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM