urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=59587): 使用 Selenium GeckoDriver Firefox 的 url 超出了最大重试次数

ant*_*sk8 6 python selenium scrapy geckodriver firefox-headless

黎明时我的代码工作得很好,但是今天当我醒来时它不再工作了,我没有更改任何代码行,我还检查了 Firefox 是否更新了,不,它没有,我没有想法可能是什么,我一直在阅读 urllib 文档,但找不到任何信息

\n
from asyncio.windows_events import NULL\nfrom ctypes.wintypes import PINT\nfrom logging import root\nfrom socket import timeout\nfrom string import whitespace\nfrom tkinter import N\nfrom turtle import color\nfrom urllib.request import Request\nfrom hyperlink import URL\nfrom selenium import webdriver\nfrom selenium.webdriver.firefox.service import Service\nfrom selenium.webdriver.firefox.options import Options\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.common.keys import Keys\nfrom selenium.webdriver.support.ui import WebDriverWait\nfrom selenium.webdriver.support.expected_conditions import presence_of_element_located\n#from webdriver_manager.firefox import GeckoDriverManager\nimport time\nfrom datetime import datetime\nimport telebot\n\n#driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))\n\ncolors = NULL\napi = "******"\nurl = "https://blaze.com/pt/games/double"\nbot = telebot.TeleBot(api)\n\nchat_id = "*****"\n\nfirefox_driver_path = "/Users/Ant\xc3\xb4nio/Desktop/roletarobo/geckodriver.exe"\nfirefox_options = Options()\nfirefox_options.add_argument("--headless")\nwebdriver = webdriver.Firefox(\nexecutable_path = firefox_driver_path,\noptions = firefox_options)\n\nwith webdriver as driver:\n\n    driver.get(url)\n    wait = WebDriverWait(driver, 25)\n\nwait.until(presence_of_element_located((By.CSS_SELECTOR, "div#roulette.page.complete")))\ntime.sleep(2)\n\nresults = driver.find_elements(By.CSS_SELECTOR, "div#roulette-recent div.entry")\nfor quote in results:\n      quote.text.split('\\n')\n\ndata = [my_elem.text for my_elem in driver.find_elements(By.CSS_SELECTOR, "div#roulette-recent div.entry")][:8]\n\n#m\xc3\xa9todo convertElements, converte elementos da lista em elementos declarados\ndef convertElements( oldlist, convert_dict ):\n    newlist = []\n    for e in oldlist:\n      if e in convert_dict:\n        newlist.append(convert_dict[e])\n      else:\n        newlist.append(e)\n    return newlist\n#fim do m\xc3\xa9todo\n\ncolors = convertElements(data, {'':"white",'1':"red",'2':"red",'3':"red",'4':"red",'5':"red",'6':"red",'7':"red",'8':"black",'9':"black",'10':"black",'11':"black",'12':"black",'13':"black",'14':"black"})\nprint(colors)\n
Run Code Online (Sandbox Code Playgroud)\n

它工作得很好,从周日开始我就一直在编码,而且它一直在工作

\n
 File "C:\\Users\\Ant\xc3\xb4nio\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\selenium\\webdriver\\support\\wait.py", line 78, in until\n    value = method(self._driver)\n  File "C:\\Users\\Ant\xc3\xb4nio\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\selenium\\webdriver\\support\\expected_conditions.py", line 64, in _predicate\n    return driver.find_element(*locator)\n  File "C:\\Users\\Ant\xc3\xb4nio\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py", line 1248, in find_element      \n    return self.execute(Command.FIND_ELEMENT, {\n  File "C:\\Users\\Ant\xc3\xb4nio\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py", line 423, in execute\n    response = self.command_executor.execute(driver_command, params)\n  File "C:\\Users\\Ant\xc3\xb4nio\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\selenium\\webdriver\\remote\\remote_connection.py", line 333, in execute    \n    return self._request(command_info[0], url, body=data)\n  File "C:\\Users\\Ant\xc3\xb4nio\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\selenium\\webdriver\\remote\\remote_connection.py", line 355, in _request   \n    resp = self._conn.request(method, url, body=body, headers=headers)        \n  File "C:\\Users\\Ant\xc3\xb4nio\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\urllib3\\request.py", line 78, in request\n    return self.request_encode_body(\n  File "C:\\Users\\Ant\xc3\xb4nio\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\urllib3\\request.py", line 170, in request_encode_body\n    return self.urlopen(method, url, **extra_kw)\n  File "C:\\Users\\Ant\xc3\xb4nio\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\urllib3\\connectionpool.py", line 813, in urlopen\n    return self.urlopen(\n  File "C:\\Users\\Ant\xc3\xb4nio\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\urllib3\\connectionpool.py", line 785, in urlopen    retries = retries.increment(\n  File "C:\\Users\\Ant\xc3\xb4nio\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\urllib3\\util\\retry.py", line 592, in increment    raise MaxRetryError(_pool, url, error or ResponseError(cause))urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=59587): Max retries exceeded with url: /session/b38be2fe-6d92-464f-a096-c43183aef6a8/element (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000173145EF520>: Failed to establish a new connection: [WinError 10061] No connections could be made because the target machine actively refused them'))\n
Run Code Online (Sandbox Code Playgroud)\n

Deb*_*anB 8

这个错误信息...

MaxRetryError(_pool, url, error or ResponseError(cause))urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=59587): Max retries exceeded with url: /session/b38be2fe-6d92-464f-a096-c43183aef6a8/element (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x00000173145EF520>: Failed to establish a new connection: [WinError 10061] No connections could be made because the target machine actively refused them'))
Run Code Online (Sandbox Code Playgroud)

...意味着GeckoDriver无法启动/生成新的浏览上下文,即会话。


根本原因

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

  • 当驱动程序已经启动对元素的查找时,如果使用强力手动关闭浏览上下文,则可能会出现此错误。
  • 您尝试访问的应用程序可能会限制来自您的系统/计算机/IP 地址/网络的请求。
  • 还有一种可能是应用程序已将Selenium驱动的GeckoDriver启动的 浏览上下文识别为,并拒绝任何访问

解决方案

确保这件事:

  • 要逃避机器人检测,请--disable-blink-features=AutomationControlled按如下方式传递参数:

    from selenium.webdriver.firefox.options import Options
    
    options = Options()
    options.add_argument('--disable-blink-features=AutomationControlled')
    
    Run Code Online (Sandbox Code Playgroud)
  • driver.quit()始终在方法内调用tearDown(){}以优雅地关闭和销毁WebDriverWeb Client实例。

  • 引发WebDriverWait将快速移动的 WebDriver 与浏览上下文同步。


归档时间:

查看次数:

42725 次

最近记录:

2 年,4 月 前