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]

    comment = page.locator("div:has-text('Experience the full naval warfare')").last
    comment.hover()
    time.sleep(1)

    dots = page.locator("div[aria-label='Edit or delete this']").first
    print("Dots visible:", dots.is_visible())
    if dots.is_visible():
        dots.click(force=True)
        time.sleep(1)

        # Inspect all elements in popups / menus
        menu_items = page.locator("div[role='menu'] *").all()
        print(f"Found {len(menu_items)} elements in menu:")
        for item in menu_items[:20]:
            txt = item.inner_text().strip()
            tag = item.evaluate("el => el.tagName")
            role = item.get_attribute("role")
            if txt:
                print(f"Tag: {tag}, role: {role}, text: {repr(txt)}")

        page.screenshot(path="FacebookAutoPoster/menu_dom_inspected.png")
