2026-07-17 12:21:31 -05:00
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
import shutil
|
|
|
|
|
import platform
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from playwright.sync_api import sync_playwright
|
|
|
|
|
|
|
|
|
|
from get_token import (
|
|
|
|
|
MENU_URL, TOKEN_FILE, SCREENSHOT_DIR,
|
|
|
|
|
fill_login_form, save_token, save_screenshot,
|
|
|
|
|
check_for_token, navigate_sol_menu, extract_token_from_menu,
|
|
|
|
|
)
|
2026-08-04 09:34:44 -05:00
|
|
|
# ============================================================================
|
2026-07-17 12:21:31 -05:00
|
|
|
USER_AGENT = (
|
|
|
|
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
|
|
|
|
|
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
|
|
|
|
"Chrome/120.0.0.0 Safari/537.36"
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-04 09:34:44 -05:00
|
|
|
# ============================================================================
|
2026-07-17 12:21:31 -05:00
|
|
|
def chrome_instalado():
|
|
|
|
|
sistema = platform.system().lower()
|
|
|
|
|
if sistema == "windows":
|
|
|
|
|
return bool(shutil.which("chrome")) or any(
|
|
|
|
|
os.path.exists(p) for p in [
|
|
|
|
|
"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
|
|
|
|
|
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
elif sistema == "darwin":
|
|
|
|
|
return os.path.exists("/Applications/Google Chrome.app")
|
|
|
|
|
elif sistema == "linux":
|
|
|
|
|
return bool(shutil.which("google-chrome") or shutil.which("google-chrome-stable"))
|
|
|
|
|
return False
|
|
|
|
|
|
2026-08-04 09:34:44 -05:00
|
|
|
# ============================================================================
|
2026-07-17 12:21:31 -05:00
|
|
|
def get_token_headless(timeout=90000):
|
|
|
|
|
with sync_playwright() as p:
|
|
|
|
|
browser = p.chromium.launch(
|
|
|
|
|
channel="chrome",
|
|
|
|
|
headless=True,
|
|
|
|
|
args=[
|
|
|
|
|
"--disable-blink-features=AutomationControlled",
|
|
|
|
|
"--disable-popup-blocking",
|
|
|
|
|
"--no-sandbox",
|
|
|
|
|
"--window-size=1280,800",
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
context = browser.new_context(
|
|
|
|
|
viewport={"width": 1280, "height": 800},
|
|
|
|
|
locale="es-PE",
|
|
|
|
|
timezone_id="America/Lima",
|
|
|
|
|
user_agent=USER_AGENT,
|
|
|
|
|
)
|
|
|
|
|
page = context.new_page()
|
|
|
|
|
page.set_default_timeout(timeout)
|
|
|
|
|
|
|
|
|
|
print("1. Sesión desde SOL...")
|
|
|
|
|
page.goto("https://www.sunat.gob.pe/sol.html", wait_until="domcontentloaded")
|
|
|
|
|
|
|
|
|
|
print("2. Navegando a menú directo...")
|
|
|
|
|
page.goto(MENU_URL, wait_until="domcontentloaded")
|
|
|
|
|
|
|
|
|
|
page.wait_for_load_state("networkidle")
|
|
|
|
|
has_inputs = len(page.locator("input[type='password']").all()) > 0
|
|
|
|
|
if has_inputs or any(k in page.url.lower() for k in ["login", "autentica", "auth"]):
|
|
|
|
|
print("3. Login detectado, rellenando credenciales...")
|
|
|
|
|
fill_login_form(page)
|
|
|
|
|
page.keyboard.press("Enter")
|
|
|
|
|
try:
|
|
|
|
|
page.wait_for_load_state("networkidle", timeout=30000)
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
page.wait_for_timeout(3000)
|
|
|
|
|
else:
|
|
|
|
|
print("3. Sin login (sesión activa)")
|
|
|
|
|
|
|
|
|
|
print("4. Buscando token en menú...")
|
|
|
|
|
page.wait_for_timeout(2000)
|
|
|
|
|
token = check_for_token(page)
|
|
|
|
|
if not token:
|
|
|
|
|
token = navigate_sol_menu(page)
|
|
|
|
|
if not token:
|
|
|
|
|
token = extract_token_from_menu(page)
|
|
|
|
|
|
|
|
|
|
context.close()
|
|
|
|
|
browser.close()
|
|
|
|
|
|
|
|
|
|
if token:
|
|
|
|
|
print(" ✅ Token obtenido (headless)")
|
|
|
|
|
save_token(token)
|
|
|
|
|
return token
|
|
|
|
|
|
|
|
|
|
print(" ❌ No se encontró token")
|
|
|
|
|
return None
|
|
|
|
|
|
2026-08-04 09:34:44 -05:00
|
|
|
# ============================================================================
|
2026-07-17 12:21:31 -05:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
get_token_headless()
|
2026-08-04 09:34:44 -05:00
|
|
|
# ============================================================================
|