7

I tried writing a simple script that checks the website every hour and sends me an email when it finds there's availability.

I figured doing this evey hour shouldn't trigger any problems but I'm getting the following error:

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'))

This is my code:

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

I'm a beginner so any help will be appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
justasking
  • 71
  • 1
  • 2
  • 5
  • 1
    Is it possible you are being rate-limited by the website in question? – Max Sep 17 '20 at 19:18
  • Thanks for the reply! I get the error the moment I start running the code, and it should only try to access the website once every hour so I don't think so. But is there a way I could verify if that's the issue? – justasking Sep 17 '20 at 21:07

2 Answers2

13

This error message...

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'))

...implies that the ChromeDriver was unable to initiate/spawn/communicate with the Browsing Context i.e. Chrome Browser session.


Root cause

The root cause of this error can be either of the following:

  • This error may arise in case manually you are closing the Browsing Context when the driver have already initiated a lookout for element/elements.
  • Incase you invoke any WebDriver methods once you have already invoked driver.close() or driver.quit().
  • There is also a possibility that the application you are trying to access is throttling the requests from your system/machine/ip-address/network.
  • The application have identified the Selenium driven ChromeDriver initiated Browsing Context as a bot and is denying any access.

Solution

Ensure that:

  • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
  • Induce WebDriverWait to synchronize the fast moving WebDriver along with the Browsing Context.
  • Selenium is upgraded to current released Version 3.141.59.
  • ChromeDriver is updated to current ChromeDriver v84.0 level.
  • Chrome is updated to current Chrome Version 84.0 level. (as per ChromeDriver v84.0 release notes)
  • If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks a lot for taking the time to respond. Everything is up to date and I'm using a virtual environment with only the required dependencies. Could you say a bit more about the first two solutions you proposed? I tried looking them up but I'm still not sure I follow. – justasking Sep 17 '20 at 20:45
  • @justasking Let us discuss this in [Selenium Chat Room](https://chat.stackoverflow.com/rooms/223360/selenium). – undetected Selenium Oct 20 '20 at 18:28
  • 1
    Thank you! this help! I couldn't solve this error with neither GitHub copilot nor GPT4 :) – Ahmed Elashry May 31 '23 at 13:48
0

You just need to move this line to the beginning of the get_stock () function

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

it's probably too late, but it might come in handy

Daniel
  • 1
  • 1