Skip to content

Instantly share code, notes, and snippets.

@boilsquid
Created November 23, 2023 17:19
Show Gist options
  • Save boilsquid/ddd1337fe9e17f6d2462a49fb2beb01f to your computer and use it in GitHub Desktop.
Save boilsquid/ddd1337fe9e17f6d2462a49fb2beb01f to your computer and use it in GitHub Desktop.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
def handler(data, context):
title = "title-placeholder"
try:
driver = create_chrome_driver()
driver.get("https://google.com/")
title = driver.title
except Exception as e:
print(e)
finally:
try:
driver.close()
except Exception as e:
print(e)
delete_core_dumps('/tmp')
return {
"title": title
}
def create_chrome_driver():
options = Options()
options.add_argument("--headless=new")
options.add_argument('--no-sandbox')
options.add_argument("--disable-gpu")
options.add_argument("--single-process")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-dev-tools")
options.add_argument('--no-cache')
options.add_argument('--disk-cache-size=0')
options.add_argument(f"--user-data-dir=/tmp/user-data")
options.add_argument(f"--data-path=/tmp/data-path")
options.add_argument(f"--disk-cache-dir=/tmp/cache-dir")
options.binary_location = '/usr/bin/chromium'
service = webdriver.ChromeService(executable_path="/usr/bin/chromedriver")
return webdriver.Chrome(options=options, service=service)
def delete_core_dumps(directory):
for file in os.listdir(directory):
if file.startswith('core.chromium'):
file_path = os.path.join(directory, file)
os.remove(file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment