Reputation: 333
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.
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.
Upvotes: 1
Views: 8403
Reputation: 193388
This error message...
('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
...implies that the ChromeDriver was unable to communicate with the WebBrowser i.e. Chrome Browser session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
Supports Chrome v67-69
Supports Chrome v74
So there is a clear mismatch between ChromeDriver v2.41 and the Chrome Browser v74.0
driver.quit()
within tearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.You can find a detailed discussion in Selenium & Heroku: urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
Upvotes: 4