"""One-shot experiment: Selenium + real Chrome survival with flag variants.""" from __future__ import annotations import time def trial(name: str, extra_args: list[str]) -> bool: from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument("--headless=new") options.add_argument("--window-size=1280,800") for arg in extra_args: options.add_argument(arg) try: driver = webdriver.Chrome(options=options) except Exception as exc: # noqa: BLE001 print(f"[{name}] LAUNCH FAILED: {str(exc)[:120]}") return False try: driver.get("https://example.com/") title1 = driver.title time.sleep(6) driver.get("https://example.com/") body = driver.find_element("css selector", "body").text[:40] print(f"[{name}] ALIVE title={title1!r} body={body!r}") return True except Exception as exc: # noqa: BLE001 print(f"[{name}] DIED after launch: {type(exc).__name__}: {str(exc)[:100]}") return False finally: try: driver.quit() except Exception as exc: # noqa: BLE001 — teardown must never mask the result print(f"[{name}] quit failed: {type(exc).__name__}: {str(exc)[:80]}") def main() -> None: import os from pathlib import Path runtime = Path(__file__).resolve().parents[3] / ".browser-runtime" cache = runtime / "selenium-cache" temp = runtime / "tmp" cache.mkdir(parents=True, exist_ok=True) temp.mkdir(parents=True, exist_ok=True) os.environ["SE_CACHE_DIRECTORY"] = str(cache) os.environ["TMP"] = str(temp) os.environ["TEMP"] = str(temp) trial("default", []) trial("no-sandbox", ["--no-sandbox", "--disable-gpu"]) if __name__ == "__main__": main()