最大重试错误Selenium

7

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

我觉得每小时执行这个操作不会引起任何问题,但是我收到了以下错误:

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

我是一名初学者,非常感谢您能提供帮助。


1
你是否被相关网站限制了访问频率? - Max
感谢回复!我在运行代码时就遇到了错误,而且它应该每小时只尝试访问一次网站,所以我不认为是这个问题。但有没有办法可以验证一下这是否是问题的原因? - justasking
2个回答

15

这个错误信息...

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浏览器会话进行通信。

根本原因

此错误的根本原因可能是以下之一:

  • 如果在驱动程序已经开始查找元素时手动关闭了浏览上下文,可能会出现此错误。
  • 如果已经调用driver.close()driver.quit(),则无论何时调用任何WebDriver 方法都会发生这种情况。
  • 您尝试访问的应用程序可能会对来自您的系统/计算机/IP地址/网络的请求进行限制
  • 该应用程序已将Selenium驱动的ChromeDriver启动的 浏览上下文识别为机器人,并且正在拒绝任何访问

解决方案

确保:

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

参考资料

您可以在以下链接中找到一些相关的详细讨论:


非常感谢您抽出时间回复。一切都是最新的,我正在使用一个虚拟环境,并且只有必需的依赖项。您能否多说一些关于您提出的前两个解决方案?我尝试查找它们,但仍然不确定我是否理解正确。 - justasking
@justasking 让我们在Selenium聊天室讨论这个问题。 - undetected Selenium
1
谢谢!这很有帮助!我无法用 GitHub copilot 或 GPT4 解决这个错误 :) - Ahmed Elashry

0

你只需要将这行代码移动到 get_stock() 函数的开头

driver = webdriver.Chrome(options=chrome_options, executable_path=PATH)

可能已经太晚了,但它可能会派上用场


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接