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.