import sys
sys.stdout.reconfigure(encoding='utf-8')
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&sk=directory_intro'
    page.goto(url, wait_until='domcontentloaded', timeout=30000)
    page.wait_for_timeout(3000)
    
    # Find button "About you"
    about_you_btn = page.locator("div[role='button']:has-text('About you')")
    print('Found about you button:', about_you_btn.count())
    if about_you_btn.count() > 0:
        print('Clicking About you button...')
        about_you_btn.first.click()
        page.wait_for_timeout(2000)
        page.screenshot(path=r'C:\Projects\Historical\FacebookAutoPoster\after_click_about_you.png')
        print('Saved after_click_about_you.png')
        
        # Check if textarea or input appeared
        textareas = page.locator('textarea')
        print('Textareas count:', textareas.count())
        inputs = page.locator('input')
        print('Inputs count:', inputs.count())
        
        # Check dialog or new buttons
        dialog = page.locator("div[role='dialog']")
        print('Dialog count:', dialog.count())
        if dialog.count() > 0:
            print('Dialog text:', dialog.inner_text())
        else:
            # Check what changed in the right panel
            print('Right panel text:')
            print(page.locator("div[role='main']").inner_text()[:500])
