app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By import time def save_directions_screenshot(start_address, end_address, output_file="directions_screenshot.png"): # Set up headless Chrome chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.add_argument("--window-size=1280,720") driver = webdriver.Chrome(options=chrome_options) try: # Format the directions URL start = start_address.replace(' ', '+') end = end_address.replace(' ', '+') url = f"https://www.google.com/maps/dir/{start}/{end}/" # Open the URL driver.get(url) time.sleep(6) # Allow map and directions to load # Click 'Accept cookies' if the button exists try: driver.find_element(By.ID, "W0wltc").click() time.sleep(1) except: pass # Take screenshot driver.save_screenshot(output_file) print(f"Directions screenshot saved as: {output_file}") finally: driver.quit() # --- Usage --- start = input("Enter starting address: ") end = input("Enter destination address: ") save_directions_screenshot(start, end) |