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()
    
    # Let's inspect the HTML of the entire left panel containing 'Intro'
    html = page.evaluate("""() => {
        const headings = document.querySelectorAll('h1, h2, h3, h4, span, div');
        for (let el of headings) {
            if (el.innerText && el.innerText.trim() === 'Details about you') {
                let parent = el.closest('div[role="main"]') || el.parentElement.parentElement;
                return parent.innerHTML;
            }
        }
        return 'Not found';
    }""")
    
    # Save a snippet or search for buttons/inputs inside
    print("Found HTML length:", len(html))
    
    # Let's find any button or link containing Bio or Pinned
    res = page.evaluate("""() => {
        const items = [];
        const all = document.querySelectorAll('*');
        for (let el of all) {
            if (el.children.length === 0 && el.innerText && (el.innerText.includes('Bio') || el.innerText.includes('Pinned') || el.innerText.includes('About you'))) {
                items.push({
                    text: el.innerText,
                    tag: el.tagName,
                    parentTag: el.parentElement.tagName,
                    parentRole: el.parentElement.getAttribute('role'),
                    parentAria: el.parentElement.getAttribute('aria-label'),
                    grandParentRole: el.parentElement.parentElement ? el.parentElement.parentElement.getAttribute('role') : null
                });
            }
        }
        return items;
    }""")
    for r in res:
        print(r)
