import sys
import time
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")
    page = browser.contexts[0].pages[0]

    # First dismiss any backdrops/notifications
    page.keyboard.press("Escape")
    time.sleep(1)
    page.keyboard.press("Escape")
    time.sleep(1)

    print("Clicking Btn 0 with force=True...")
    btn0 = page.locator("div[role='main'] div[role='button']:has-text(\"What's on your mind\")").first
    btn0.click(force=True)
    time.sleep(2)

    page.screenshot(path="FacebookAutoPoster/inspect_wom_dialog.png")
    print("Screenshot saved: inspect_wom_dialog.png")

    dialogs = page.locator("div[role='dialog']").all()
    for i, d in enumerate(dialogs):
        if d.is_visible():
            print(f"Visible Dialog {i}:")
            btns = d.locator("div[role='button'], button").all()
            for bi, b in enumerate(btns):
                al = b.get_attribute("aria-label")
                txt = b.inner_text().strip().replace('\n', ' ')
                print(f"  Btn {bi}: aria-label={repr(al)}, text={repr(txt)}")
            tbs = d.locator("div[role='textbox']").all()
            for ti, tb in enumerate(tbs):
                al = tb.get_attribute("aria-label")
                print(f"  Textbox {ti}: aria-label={repr(al)}")
