import sys
from playwright.sync_api import sync_playwright

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

GROUP_URL = "https://www.facebook.com/groups/835457716553897/"

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp("http://127.0.0.1:9224")
    context = browser.contexts[0]
    page = context.pages[0] if context.pages else context.new_page()

    page.goto(GROUP_URL, wait_until="domcontentloaded")
    page.wait_for_timeout(3000)

    # Click xem bình luận bài 1 (TÌM THẦY DẠY LÁI XE)
    comment_btns = page.locator('div[role="button"][aria-label*="bình luận"], div[role="button"][aria-label*="comment"], div[role="button"]:has-text("bình luận"), div[role="button"]:has-text("comment")')
    print("Found comment buttons:", comment_btns.count())

    # Lấy text bài viết đầu tiên
    first_article = page.locator("div[role='feed'] > div").nth(1)
    print("\n--- NỘI DUNG BÀI VIẾT 1 ---")
    print(first_article.inner_text()[:400])

    # Lấy text bài viết thứ hai/ba
    second_article = page.locator("div[role='feed'] > div").nth(2)
    print("\n--- NỘI DUNG BÀI VIẾT 2 ---")
    print(second_article.inner_text()[:400])
