import sys
from pathlib import Path
from playwright.sync_api import sync_playwright

sys.stdout.reconfigure(encoding="utf-8")

user_data = r"C:\Users\Admin\AppData\Local\Google\Chrome\User Data"

print("Testing Playwright with Chrome Profile 5...")
try:
    with sync_playwright() as p:
        args = [
            "--profile-directory=Profile 5",
            "--disable-blink-features=AutomationControlled",
            "--exclude-switches=enable-automation",
            "--no-first-run",
            "--no-default-browser-check",
            "--disable-infobars",
        ]
        context = p.chromium.launch_persistent_context(
            user_data_dir=user_data,
            channel="chrome",
            headless=True,
            args=args,
            ignore_default_args=["--enable-automation"]
        )
        page = context.pages[0] if context.pages else context.new_page()
        page.goto("https://muse.ai", wait_until="networkidle")
        page.wait_for_timeout(3000)
        
        print("Page URL:", page.url)
        print("Page Title:", page.title())
        page.screenshot(path="scratch_pw_muse.png")
        print("Screenshot saved to scratch_pw_muse.png")
        
        # Check text
        body = page.locator("body").inner_text()
        print("Body excerpt:\n", body[:300])
        context.close()
except Exception as e:
    print("Error:", e)
