简体   繁体   中英

'Connection aborted.', ConnectionResetError(104, 'Connection reset by peer') using Selenium with ChromeDriver and Chrome through Python

The code below is executed in a loop, where 10-15 local .html files are opened and an image of each is saved as a .png.

  • Ubuntu Server 16.04
  • ChromeDriver 2.41.578700
  • Google Chrome 74.0.3729.108
  • selenium 3.141.0
  • Python 3.6

The first two files are opened and the image is saved, however the rest result in:

('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

The paths to the files are all correct and changing the order of the images to be saved does not make a difference.

def _save_image(html_file_path, png_file_path, h=850, w=833):
    try:
        from selenium import webdriver
        from selenium.webdriver.chrome.options import Options
    except Exception as ex:
        raise Exception("Saving the plot as a .PNG requires *selenium* package to be installed. Please install selenium using *pip install selenium*.")

    options = Options()
    options.add_argument('--headless')
    options.add_argument('disable-infobars')
    options.add_argument('--disable-extensions')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    #options.add_argument('--disable-gpu')

    if os.name == 'nt':
        chrome_driver_path = os.path.dirname(__file__)
        chrome_driver_path = chrome_driver_path[:-3] + "chromedriver.exe"
    elif os.name == 'posix':
        chrome_driver_path = "/usr/bin/chromedriver"
    else:
        raise Exception("OS could not be detected, thus selenium could not be initialised properly.")
    driver = webdriver.Chrome(chrome_driver_path, chrome_options=options)
    driver.set_window_size(w, h)
    driver.get("file://"+html_file_path)
    time.sleep(5)
    driver.save_screenshot(png_file_path + ".png")
    driver.quit()
    time.sleep(5)

The time.sleep(5) was added to check if the error was due to the page taking long to load, increased it to 30 seconds and the result was the same. The import statements are within the function due to a technical requirement which will be sorted at a later stage.

This error message...

('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

...implies that the ChromeDriver was unable to communicate with the WebBrowser ie Chrome Browser session.

Your main issue is the incompatibility between the version of the binaries you are using as follows:

  • You are using chromedriver=2.41
  • Release Notes of chromedriver=2.41 clearly mentions the following :

Supports Chrome v67-69

  • You are using chrome=74.0
  • Release Notes of ChromeDriver v74.x clearly mentions the following :

Supports Chrome v74

So there is a clear mismatch between ChromeDriver v2.41 and the Chrome Browser v74.0


Solution

You can find a detailed discussion in Selenium & Heroku: urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))

This most probably because you are using an older version of chrome driver for new Chrome version.

Download the latest chrome driver for your Chrome from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM