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]
    
    print("Clicking Continue...")
    btn = page.locator("div[role='button']:has-text('Continue'), button:has-text('Continue')").first
    if btn.is_visible():
        btn.click()
        time.sleep(4)
        print("New URL:", page.url)
        print("New Page Text:", page.locator("body").inner_text()[:600].replace('\n', ' '))
        page.screenshot(path="FacebookAutoPoster/checkpoint_step2.png")
        print("Saved checkpoint_step2.png")
    else:
        print("Continue button not visible!")
