from pathlib import Path import shutil from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service output_dir = Path("artifacts") output_dir.mkdir(exist_ok=True) output_file = output_dir / "example-home.png" options = Options() options.add_argument("--headless=new") options.add_argument("--window-size=1280,720") for browser_name in ("google-chrome", "chromium", "chromium-browser"): browser_path = shutil.which(browser_name) if browser_path: options.binary_location = browser_path break driver_path = shutil.which("chromedriver") service = Service(driver_path) if driver_path else None driver = webdriver.Chrome(service=service, options=options) try: driver.get("https://example.com/") saved = driver.save_screenshot(str(output_file)) print(f"title: {driver.title}") print(f"screenshot_saved: {saved}") print(f"path: {output_file}") finally: driver.quit()