import sys
import time
from pathlib import Path
from invisible_playwright import InvisiblePlaywright

if sys.platform == "win32":
    sys.stdout.reconfigure(encoding="utf-8", line_buffering=True)
    sys.stderr.reconfigure(encoding="utf-8", line_buffering=True)

pdir = Path.home() / 'AntigravityProfiles' / 'phu2234_worker'

with InvisiblePlaywright(profile_dir=pdir, headless=True) as ctx:
    page = ctx.pages[0] if ctx.pages else ctx.new_page()
    page.goto('https://muse.ai', wait_until='networkidle', timeout=30000)
    time.sleep(3)
    
    # In ra HTML của element [1]
    res = page.evaluate('''() => {
        const elements = Array.from(document.querySelectorAll('button, div[role="button"], a, div'));
        const found = elements.filter(el => el.innerText?.trim() === 'Tạo video cận cảnh mắt hoàng hôn');
        if (!found.length) return "Not found";
        const el = found[0];
        return {
            tag: el.tagName,
            class: el.className,
            parentTag: el.parentElement?.tagName,
            parentClass: el.parentElement?.className,
            parentHTML: el.parentElement?.outerHTML?.slice(0, 300)
        };
    }''')
    import json
    print(json.dumps(res, indent=2))
