from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    cdp = p.chromium.connect_over_cdp('http://localhost:9223')
    ctx = cdp.contexts[0]
    page = ctx.pages[0]
    page.goto('https://grok.com/imagine/post/23dd7313-4a69-483b-ba8c-d3427f6603f6')
    page.wait_for_timeout(3000)

    try:
        page.locator('[aria-label="Permitirlas todas"]').click(timeout=2000)
        page.wait_for_timeout(500)
    except:
        pass

    all_btns = page.evaluate("""
        () => {
            const els = Array.from(document.querySelectorAll("[aria-label='Descargar']"));
            
            // Look for any element with Descargar or Download in aria-label
            const allWithLabel = Array.from(document.querySelectorAll("*")).filter(el => {
                const label = el.getAttribute("aria-label") || "";
                return label.includes("Descarg") || label.includes("Download");
            });

            return JSON.stringify({
                descargar_count: els.length,
                all_label_count: allWithLabel.length,
                items: els.map(el => ({
                    tag: el.tagName,
                    aria: el.getAttribute("aria-label"),
                    rect: JSON.stringify(el.getBoundingClientRect()),
                    display: window.getComputedStyle(el).display,
                    visibility: window.getComputedStyle(el).visibility,
                    offsetParent: !!el.offsetParent,
                    classes: el.className.slice(0, 50)
                }))
            });
        }
    """)
    print('Buttons:', all_btns)
    cdp.close()