from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp('http://localhost:9222')
    page = browser.contexts[0].pages[0]
    
    # Tìm link của Seven Nguyen ở top bar hoặc avatar
    links = page.locator("a[href*='facebook.com/']").all()
    target_url = None
    for a in links:
        h = a.get_attribute('href')
        txt = a.inner_text().strip()
        if 'Seven Nguyen' in txt and '/about' not in h:
            target_url = h
            break
            
    print('Found Profile Target URL:', target_url)
    if target_url:
        page.goto(target_url, wait_until='domcontentloaded', timeout=30000)
        page.wait_for_timeout(3000)
        print('Navigated to:', page.url)
        page.screenshot(path=r'C:\Projects\Historical\FacebookAutoPoster\root_profile.png')
        print('Root profile saved!')
    browser.close()
