File size: 1,864 Bytes
6eb6c4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""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()