File size: 2,832 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""One-shot experiment: does Selenium-driven Firefox survive on this machine?



Playwright's juggler-piped Firefox aborts ~2.6s after launch (denied child

spawns -> 0xC0000005). Selenium launches Firefox through geckodriver with a

normal startup path — same binary, no juggler. There is no installed Mozilla

Firefox on this box, so the experiment points Selenium at the Playwright-

downloaded firefox.exe (same engine build).

"""
from __future__ import annotations

import time
from pathlib import Path

FIREFOX_BIN = (
    Path(__file__).resolve().parents[3]
    / ".browser-runtime" / "playwright-browsers" / "firefox-1555" / "firefox" / "firefox.exe"
)

MOZ_ENV = {
    "MOZ_DISABLE_CONTENT_SANDBOX": "1",
    "MOZ_DISABLE_GFX_SANDBOX": "1",
    "MOZ_DISABLE_RDD_SANDBOX": "1",
    "MOZ_DISABLE_SOCKET_PROCESS_SANDBOX": "1",
}


def main() -> int:
    import os

    from selenium import webdriver

    # Selenium Manager caches drivers under ~/.cache/selenium and extracts its
    # helper under %TEMP% — both access-denied for this account. Redirect both
    # into the app runtime root (same workaround as the Playwright manager).
    runtime = FIREFOX_BIN.parents[2]
    cache_dir = runtime / "selenium-cache"
    temp_dir = runtime / "tmp"
    for directory in (cache_dir, temp_dir):
        directory.mkdir(parents=True, exist_ok=True)
    os.environ["SE_CACHE_DIRECTORY"] = str(cache_dir)
    os.environ["TMP"] = str(temp_dir)
    os.environ["TEMP"] = str(temp_dir)

    if not FIREFOX_BIN.exists():
        print(f"firefox binary missing: {FIREFOX_BIN}")
        return 2
    options = webdriver.FirefoxOptions()
    options.binary_location = str(FIREFOX_BIN)
    options.add_argument("-headless")
    options.set_preference("layers.gpu-process.enabled", False)
    options.set_preference("gfx.webrender.software", True)

    driver = webdriver.Firefox(options=options)
    try:
        driver.get("https://example.com/")
        title = driver.title
        time.sleep(6)  # outlive the ~2.6s playwright-path bomb
        alive = "no exception on any command"
        driver.get("https://example.com/")  # second navigation after the window
        title2 = driver.title
        body = driver.find_element("css selector", "body").text[:60]
        print(f"ALIVE after 6s | title={title!r}/{title2!r} | body={body!r} | {alive}")
        return 0
    except Exception as exc:  # noqa: BLE001
        print(f"DIED: {type(exc).__name__}: {str(exc)[:150]}")
        return 1
    finally:
        try:
            driver.quit()
        except Exception as exc:  # noqa: BLE001 — teardown must never mask the result
            print(f"quit failed: {type(exc).__name__}: {str(exc)[:80]}")


if __name__ == "__main__":
    raise SystemExit(main())