【问题标题】:MaxRetryError SeleniumMaxRetryError 硒
【发布时间】:2021-01-04 17:27:36
【问题描述】:

我尝试编写一个简单的脚本,每小时检查一次网站,并在发现可用时向我发送电子邮件。

我认为这个每一小时不应该引发任何问题,但我收到以下错误:

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

我是一个初学者,所以任何帮助将不胜感激。

【问题讨论】:

  • 您是否可能受到相关网站的速率限制?
  • 感谢您的回复!当我开始运行代码时,我得到了错误,它应该只尝试每小时访问一次网站,所以我不这么认为。但是有没有办法我可以验证这是否是问题所在?

标签: python selenium google-chrome selenium-chromedriver httpconnection


【解决方案1】:

此错误消息...

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 无法启动/生成/与 Browsing ContextChrome 浏览器 会话进行通信。 p>


根本原因

此错误的根本原因可能是以下任何一种:

  • 如果您手动关闭浏览上下文,而驱动程序已经启动了对元素的查找,则可能会出现此错误。
  • 如果您调用了任何WebDriver methods,则您已经调用了driver.close()driver.quit()
  • 您尝试访问的应用程序也有可能是throttling 来自您的系统/机器/IP 地址/网络的请求。
  • 应用程序已将Selenium 驱动的ChromeDriver 启动 浏览上下文 识别为机器人并且是denying any access

解决方案

确保:

  • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例。
  • 诱导WebDriverWait 将快速移动的WebDriver 与浏览上下文同步。
  • Selenium 升级到当前发布的Version 3.141.59
  • ChromeDriver 已更新到当前的ChromeDriver v84.0 级别。
  • Chrome 已更新到当前的 Chrome 版本 84.0 级别。 (根据ChromeDriver v84.0 release notes
  • 如果您的基本 Web 客户端 版本太旧,请卸载它并安装最新的 GA 和发布版本的 Web 客户端
  • 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。

参考文献

您可以在以下位置找到一些相关的详细讨论:

【讨论】:

  • 非常感谢您花时间回复。一切都是最新的,我正在使用一个只有所需依赖项的虚拟环境。你能多说一下你提出的前两个解决方案吗?我尝试查找它们,但我仍然不确定我是否遵循。
  • @justasking 让我们在Selenium Chat Room 讨论这个问题。
猜你喜欢
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 1970-01-01
  • 2019-10-12
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多