import sys
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp('http://localhost:9222')
    context = browser.contexts[0]
    page = context.pages[0] if context.pages else context.new_page()
    
    url = 'https://www.facebook.com/profile.php?id=61594841053758'
    print('Navigating to profile root...')
    page.goto(url, wait_until='domcontentloaded', timeout=30000)
    page.wait_for_timeout(4000)
    
    main_el = page.locator('div[role="main"]')
    if main_el.count() > 0:
        main_text = main_el.inner_text()
        print('=== MAIN PROFILE TEXT ===')
        for line in main_text.split('\n'):
            if line.strip():
                print(line.strip())
    else:
        print('No div[role="main"] found')
        
    page.screenshot(path=r'C:\Projects\Historical\FacebookAutoPoster\profile_live.png')
    print('Screenshot profile_live.png saved!')
