python - 每次触发 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 捕获异常也会发生这种情况。
为什么会发生此错误以及如何防止它发生?
解决方案
推荐阅读
- ajax - 在 Epic 中测试 Post 请求
- javascript - NgClass 问题 Angular 和 D3
- android - 如何避免在任务完成之前导航到下一个?
- php - 如何在 php 中使用 PDFTK 背景?
- node.js - 将graphql apollo服务器部署到heroku
- typescript - 如何在 Typescript 中将对象与接口进行比较以查看对象是否具有正确的变量类型?
- javascript - 在我的颤动应用程序中输入时无法使键盘向上推动屏幕
- java - 如何通过 Android 的改造在相同的 KEY 中获取 Json 对象和 Json 数组
- apache - 重定向 CSS 过多
- laravel - Laravel 8 Socialite 检查登录