首页 > 解决方案 > 每次触发 KeyboardInterrupt 或其他错误后,Selenium 驱动程序都会因 MaxRetryError 而崩溃

问题描述

如果满足特定条件,我有一个在网站上执行少量操作的脚本。我正在使用无头硒 Chrome 和 Firefox。我在 Jupyter Notebook 中运行这个脚本。该应用程序按预期工作,几乎没有错误。仅当我需要停止脚本或它被其他错误中断时才会出现问题。之后,司机碾压。

while True:
    a, b = get_a_and_b()
    if a > b:
           print ("click a button")
           driver.find_element_by_class_name("button").click()
    sleep(1)

函数 get_a_and_b() 与 selenium 没有交互。但是,当我启动 KeyboardInterrupt 时,函数会停止,但之后的驱动程序会返回 MaxRetryError:

Errr HTTPConnectionPool(host='127.0.0.1', port=33111): Max retries exceeded with url: /session/4d754ba9-0c65-4c40-b045-a8e8432b4564/window (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f390b09beb0>: Failed to establish a new connection: [Errno 111] Connection refused'))
'MaxRetryError' object has no attribute 'code'

之后我需要再次启动驱动程序,因为它没有响应任何内容。即使我用 try 捕获异常也会发生这种情况。

为什么会发生此错误以及如何防止它发生?

标签: pythonseleniumjupyter-notebook

解决方案


推荐阅读