import sys from tempfile import mkdtemp from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.service import Service
defhandler(event, context): print(event) # print(context) print('Hello from AWS Lambda using Python' + sys.version + '!')
options = webdriver.ChromeOptions() options.binary_location = CHROMIUM_PATH options.add_argument("--headless") # Hide the GUI options.add_argument("--no-sandbox") # No protection needed options.add_argument("--window-size=1280x1696") # Setup a fixed screens size options.add_argument("--single-process") # Lambda only give us only one CPU options.add_argument("--no-zygote") # Don't create zygote processes because Lambda give us only one CPU options.add_argument("--disable-dev-shm-usage") # Create temporary folder for shared memory files options.add_argument("--disable-dev-tools") # Disable Chrome dev tools options.add_argument(f"--user-data-dir={mkdtemp()}") # Create temporary folder to user data options.add_argument(f"--data-path={mkdtemp()}") # Create temporary folder to browser data options.add_argument(f"--disk-cache-dir={mkdtemp()}") # Create temporary folder to cache
service = Service(CHROMEDRIVER_PATH) driver = webdriver.Chrome(service=service, options=options)
options.add_experimental_option('prefs', { "download.default_directory": tmp_dir, # Change default directory for downloads "download.prompt_for_download": False, # To auto download the file "download.directory_upgrade": True, "plugins.always_open_pdf_externally": True# It will not show PDF directly in chrome })