问题描述
我尝试编写一个简单的脚本,该脚本每小时检查一次网站,并在发现可用时向我发送电子邮件。
我认为执行此举不应该触发任何问题,但是出现以下错误:
MaxRetryError: httpconnectionPool(host='127.0.0.1',port=60745): Max retries exceeded with url: /session/900f45d6c8c800f2a8ebcf43daa05b69/url (Caused by NewConnectionError('<urllib3.connection.httpconnection object at 0x7fa42c261c10>: Failed to establish a new connection: [Errno 61] Connection refused'))
这是我的代码:
import schedule
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from notification import *
#script i have to send an email (works fine)
PATH = "mypath"
# i have the path where there drivers are
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
# to not open the browser
driver = webdriver.Chrome(options=chrome_options,executable_path=PATH)
def get_stock():
driver.get("website i'm trying to check")
# access the website
search = driver.find_element_by_name("add")
# add is the name of the button i wanna check
result = search.is_enabled()
print(result)
driver.quit()
schedule.every().hour.do(get_stock)
# run the get_stock function every hour
c = 0
# initialize the loop
while c == 0:
schedule.run_pending()
c = get_stock()
# set the seed equal to the get_stock so that it stops once it becomes True
time.sleep(1)
print(get_stock())
email("Now there's a stock.")
#using my notification script to send the email
我是一个初学者,因此将不胜感激。
解决方法
此错误消息...
MaxRetryError: HTTPConnectionPool(host='127.0.0.1',port=60745): Max retries exceeded with url: /session/900f45d6c8c800f2a8ebcf43daa05b69/url (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fa42c261c10>: Failed to establish a new connection: [Errno 61] Connection refused'))
...表示 ChromeDriver 无法与浏览上下文即 Chrome浏览器会话启动/生成/通信。>
根本原因
此错误的根本原因可能是以下之一:
- 如果驱动程序已经启动了元素/元素的查找,则当您手动关闭浏览上下文时,可能会出现此错误。
- 如果您已经调用
driver.close()
或driver.quit()
后调用了任何WebDriver methods。 - 您尝试访问的应用程序也有可能是throttling来自您的系统/机器/ ip-address /网络的请求。
- 该应用已将Selenium驱动的ChromeDriver驱动的google-chrome 浏览上下文识别为漫游器,并且是denying any access。
解决方案
确保:
- 始终在
driver.quit()
方法内调用tearDown(){}
,以优雅地关闭和销毁 WebDriver 和 Web Client 实例。 - 诱导WebDriverWait以使快速移动的WebDriver与浏览上下文同步。
- 硒已升级到当前发布的Version 3.141.59。
- ChromeDriver 已更新为当前的ChromeDriver v84.0级别。
- Chrome 已更新为当前的 Chrome版本84.0 级别。 (根据ChromeDriver v84.0 release notes)
- 如果您的基本 Web客户端版本太旧,则将其卸载并安装最新版本的 Web客户端 GA。 通过 IDE
- 清理项目项目工作区,并仅使用必需的依赖项重新构建项目。
参考文献
您可以在以下位置找到几个相关的详细讨论: