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_work'
    page.goto(url, wait_until='domcontentloaded', timeout=30000)
    page.wait_for_timeout(3000)
    
    # Click "Work experience" button
    work_btn = page.locator("div[role='button']:has-text('Work experience'), span:has-text('Work experience')").first
    print('Found Work experience button. Clicking...')
    work_btn.click()
    page.wait_for_timeout(2000)
    page.screenshot(path=r'C:\Projects\Historical\FacebookAutoPoster\work_form_opened.png')
    
    # Print inputs and text in the form
    inputs = page.locator("input, textarea")
    print('Inputs count:', inputs.count())
    for i in range(inputs.count()):
        inp = inputs.nth(i)
        aria = inp.get_attribute('aria-label') or ''
        placeholder = inp.get_attribute('placeholder') or ''
        name = inp.get_attribute('name') or ''
        inp_type = inp.get_attribute('type') or ''
        print(f"Input [{i}]: type='{inp_type}' aria='{aria}' placeholder='{placeholder}' name='{name}'")
