#!/usr/bin/env python3
"""Test Playwright pagination for xiangfuqu"""
from playwright.sync_api import sync_playwright
import sys, json
sys.stdout.reconfigure(encoding='utf-8')

with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=True)
    page = browser.new_page()
    page.goto('https://www.xiangfuqu.gov.cn/kfsxfqwz/c00511/pc/list.html', 
              wait_until='networkidle', timeout=30000)
    
    # Get all items
    items = page.eval_on_selector_all('a[href*="content_"]', 
        'els => els.map(el => ({title: el.textContent.trim(), href: el.href}))')
    
    print(f"Page 1: {len(items)} items")
    for item in items[:5]:
        print(f"  {item['title'][:50]} -> {item['href'][:60]}")
    
    # Check for pagination
    pagination = page.eval_on_selector_all('.page *, .pagination *, [class*="page"], [class*="Page"]',
        'els => els.map(el => ({tag: el.tagName, text: el.textContent.trim(), class: el.className}))')
    
    if pagination:
        print(f"\nPagination elements:")
        for p in pagination[:20]:
            if p['text'] and len(p['text']) < 10:
                print(f"  <{p['tag']}> {p['text']} (class: {p['class']})")
    
    # Also check for page numbers
    page_numbers = page.eval_on_selector_all('a[href*="list_"]',
        'els => els.map(el => ({text: el.textContent.trim(), href: el.href}))')
    
    if page_numbers:
        print(f"\nPage links:")
        for pn in page_numbers[:10]:
            print(f"  {pn['text']} -> {pn['href']}")
    
    # Check total
    total_text = page.text_content('body') or ''
    import re
    m = re.search(r'共(\d+)条', total_text)
    if m:
        print(f"\nTotal: {m.group(1)}条")
    
    browser.close()
