import sys
from playwright.sync_api import sync_playwright

if hasattr(sys.stdout, 'reconfigure'):
    sys.stdout.reconfigure(encoding='utf-8')

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp('http://127.0.0.1:9224')
    context = browser.contexts[0]
    page = None
    for pg in context.pages:
        if 'facebook.com' in pg.url:
            page = pg
            break
    if not page:
        page = context.new_page()
    page.goto('https://www.facebook.com/', wait_until='domcontentloaded')
    page.wait_for_timeout(3000)
    
    # Scroll slightly to load feed posts
    page.mouse.wheel(0, 800)
    page.wait_for_timeout(3000)
    
    leave_comment_btn = page.locator('div[role="button"][aria-label*="Leave a comment"], div[role="button"][aria-label*="Bình luận"]').first
    if leave_comment_btn.is_visible():
        print("Clicking Leave a comment button...")
        leave_comment_btn.click()
        page.wait_for_timeout(2000)
        
        # Check active element and textboxes
        active_tag = page.evaluate('() => document.activeElement ? document.activeElement.tagName + " " + document.activeElement.getAttribute("aria-label") : "none"')
        print("Active element after click:", active_tag)
        
        textboxes = page.locator('div[role="textbox"][contenteditable="true"]')
        print(f"Total editable textboxes after click: {textboxes.count()}")
        for j in range(textboxes.count()):
            tb = textboxes.nth(j)
            print(f"  Textbox [{j}]: aria-label='{tb.get_attribute('aria-label')}'")


