import sys, time
from playwright.sync_api import sync_playwright

sys.stdout.reconfigure(encoding="utf-8")
user_data = r"C:\Users\Admin\.chrome_muse_phu2234"

print("[1] Launching Playwright with non-default user_data_dir...", flush=True)
try:
    with sync_playwright() as p:
        context = p.chromium.launch_persistent_context(
            user_data_dir=user_data,
            channel="chrome",
            headless=True,
            args=[
                "--disable-blink-features=AutomationControlled",
                "--no-first-run",
                "--no-default-browser-check",
            ]
        )
        print("[2] Browser context opened successfully!", flush=True)
        page = context.pages[0] if context.pages else context.new_page()
        
        print("[3] Navigating to https://muse.ai...", flush=True)
        page.goto("https://muse.ai", wait_until="domcontentloaded", timeout=25000)
        time.sleep(5)
        
        print(f"[4] Current URL: {page.url} | Title: {page.title()}", flush=True)
        page.screenshot(path="scratch_pw_dedicated.png")
        print("[5] Screenshot saved to scratch_pw_dedicated.png", flush=True)
        
        body = page.locator("body").inner_text()
        print(f"[6] Body preview:\n{body[:300]}", flush=True)
        context.close()
        print("[7] Done!", flush=True)
except Exception as e:
    import traceback
    traceback.print_exc()
