-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathplaywright_setup.py
More file actions
37 lines (29 loc) · 1.38 KB
/
playwright_setup.py
File metadata and controls
37 lines (29 loc) · 1.38 KB
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
import subprocess
import sys
import os
from loguru import logger
def ensure_playwright_installed(browser: str = "chromium"):
"""
Проверяет наличие браузеров Playwright и переопределяет путь для exe-сборки.
Устанавливает их при необходимости.
"""
try:
# === Указываем правильный путь к браузерам ===
ms_playwright_dir = os.path.join(
os.path.expanduser("~"), "AppData", "Local", "ms-playwright"
)
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = ms_playwright_dir
from playwright._impl._driver import compute_driver_executable
result = compute_driver_executable()
if isinstance(result, tuple):
driver_path, _ = result
else:
driver_path = result
browsers_exist = os.path.exists(driver_path) or os.path.exists(ms_playwright_dir)
if not browsers_exist:
logger.info(f"Playwright не найден. Устанавливаю {browser}...")
subprocess.run([sys.executable, "-m", "playwright", "install", browser], check=True)
else:
logger.debug("Playwright уже установлен, хорошо")
except Exception as e:
logger.warning(f"Ошибка при установке\проверке Playwright: {e}")