<jupyter_start><jupyter_text>Script for Getting Screenshots of Route Maps<NAME> under the MIT licenseThis program shows how to use selenium to generate .png versions of the .html route maps generated by route_maps_builder_v2. Automating this screenshot process is much easier than getting manual screenshots of each map.<jupyter_code>import os

from selenium import webdriver
# See https://www.selenium.dev/documentation/
ff_driver = webdriver.Firefox()
# For the above line to execute correctly in Windows,
# you'll need to download geckodriver, then add the path to the folder
# containing the .exe file to your system environment variable.

# ALternately, you could specify the path to the geckodriver.exe file within
# webdriver.Firefox(), as follows::
# ff_driver = webdriver.Firefox(
#     executable_path=r'C:\Users\kburc\Downloads\geckodriver-v0.30.0-win64\geckodriver.exe')
    # This was based on Nesa's answer at 
    # https://stackoverflow.com/a/42122284/13097194



# ff_driver.set_window_position(0,0)
# See https://www.selenium.dev/documentation/webdriver/browser/windows/#set-window-size 
# for set_window_position and set_window_size
window_width = 2200 # I experimented with this setting until I found a
# dimension that produced decent (if not perfect) representations of 
# the domestic and international route map views. Interestingly, this
# setting (when plugged into set_window_size below)
# resulted in dimensions of 4378 x 2294 pixels. This may be because I have
# my monitor set to 200% scaling. At any rate, the output (4378 x 2294) produced
# pretty good image files.
ff_driver.set_window_size(window_width,window_width*(9/16))
# 9/16 is used to maintain the 16:9 aspect ratio seen in HD, 4K, etc. screens.
# ff_driver.set_window_size(3840,2160) resulted in a 7658x4140 image for me
import time


path_to_maps_folder = \
r'C:\Users\kburc\D1V1\Documents\!Dell64docs\Programming\py\kjb3_programs\route_maps_builder\folium_maps'
for root, dirs, files in os.walk('folium_maps'): 
# See https://docs.python.org/3/library/os.html

    file_list = files
file_list
file_list[0][:-4]
for i in range(len(file_list)):

    ff_driver.get(path_to_maps_folder+'\\'+file_list[i]) # See https://www.selenium.dev/documentation/webdriver/browser/navigation/
    time.sleep(2) # This gives the page sufficient
    # time to load the map tiles before the screenshot is taken.
    

    screenshot_test = ff_driver.get_screenshot_as_file(
        'folium_map_screenshots\\'+file_list[i][:-4]+'png') 
    # Based on https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/TakesScreenshot.html
    # The file name equals the name of the .html file of each route map (except
    # for the 'html' component) plus png to represent the image type.

ff_driver.quit()
# Based on https://www.selenium.dev/documentation/webdriver/browser/windows/<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup