import sys, time
from invisible_playwright import InvisiblePlaywright

sys.stdout.reconfigure(encoding="utf-8")
profile = r"C:\Users\Admin\AntigravityProfiles\phu2234"

print(f"[1] Launching InvisiblePlaywright with profile: {profile}...", flush=True)
try:
    with InvisiblePlaywright(profile_dir=profile, headless=True) as ctx:
        print("[2] Context ready! Opening page...", flush=True)
        page = ctx.pages[0] if ctx.pages else ctx.new_page()
        
        print("[3] Navigating to https://muse.ai...", flush=True)
        page.goto("https://muse.ai", wait_until="domcontentloaded", timeout=30000)
        print(f"[4] Loaded URL: {page.url} | Title: {page.title()}", flush=True)
        
        time.sleep(3)
        print(f"[5] Post-wait URL: {page.url}", flush=True)
        
        # Check text or elements on page
        body_text = page.locator("body").inner_text()
        print(f"[6] Body text preview (first 300 chars):\n{body_text[:300]}", flush=True)
        
        page.screenshot(path="scratch_invpw_muse.png")
        print("[7] Screenshot saved to scratch_invpw_muse.png", flush=True)
except Exception as e:
    import traceback
    traceback.print_exc()
