Use packaged Chromium driver in Docker

This commit is contained in:
2026-07-28 22:15:14 +03:30
parent 4462990d6f
commit 3982b03f5c
4 changed files with 50 additions and 21 deletions
+35 -2
View File
@@ -7,6 +7,7 @@ version matching the installed Chrome, then patches it with undetected_chromedri
from __future__ import annotations
import io
import re
import shutil
import stat
import subprocess
@@ -42,7 +43,10 @@ def _get_chrome_full_version() -> tuple[int, str]:
out = subprocess.check_output(
[binary, "--version"], stderr=subprocess.DEVNULL, text=True, timeout=5
).strip()
full = out.split()[-1]
match = re.search(r"\b(\d+(?:\.\d+){3})\b", out)
if match is None:
continue
full = match.group(1)
major = int(full.split(".")[0])
log.info("Detected Chrome %s via '%s'", full, binary)
return major, full
@@ -97,13 +101,42 @@ def _patch(dest: Path) -> None:
log.info("Patch applied successfully")
def _validate_existing_driver(dest: Path, chrome_major: int) -> bool:
if not dest.exists():
return False
try:
out = subprocess.check_output(
[str(dest), "--version"], stderr=subprocess.DEVNULL, text=True, timeout=5
).strip()
driver_major = int(out.split()[1].split(".")[0])
except Exception as exc:
log.critical("Could not inspect ChromeDriver at %s: %s", dest, exc)
sys.exit(1)
if driver_major != chrome_major:
log.critical(
"ChromeDriver major %d does not match Chrome major %d",
driver_major,
chrome_major,
)
sys.exit(1)
log.info("Using packaged ChromeDriver %s", out.split()[1])
return True
def main() -> None:
dest = Path(config.chromedriver_path)
dest.parent.mkdir(parents=True, exist_ok=True)
major, _ = _get_chrome_full_version()
cd_version = _latest_chromedriver_version(major)
if _validate_existing_driver(dest, major):
_patch(dest)
log.info("ChromeDriver ready at %s", dest)
return
cd_version = _latest_chromedriver_version(major)
_download_chromedriver(cd_version, dest)
_patch(dest)