#!/usr/bin/env python3
"""诊断翻页问题"""
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as pw:
        browser = await pw.chromium.launch(headless=True)
        page = await browser.new_page()
        
        # 第1页
        await page.goto('https://www.yanshougs.com/list/2.html', timeout=30000, wait_until='domcontentloaded')
        await page.wait_for_timeout(5000)
        rows1 = await page.evaluate('document.querySelectorAll("table tbody tr").length - 1')
        print(f'第1页: {rows1} 条')
        
        # 打印分页链接
        links = await page.evaluate("""() => {
            const sel = document.querySelector('.pagination');
            if (!sel) return 'no-pagination';
            return Array.from(sel.querySelectorAll('a')).map(a => ({
                text: a.textContent.trim(),
                href: a.getAttribute('href')
            }));
        }""")
        print(f'分页: {links}')
        
        # 尝试JS点击第2页
        clicked = await page.evaluate("""() => {
            const link = document.querySelector('a[href="/publicity_list?page=2"]');
            if (link) { link.click(); return true; }
            return false;
        }""")
        print(f'点击返回: {clicked}')
        
        await page.wait_for_timeout(8000)
        
        # 打印当前URL
        print(f'当前URL: {page.url}')
        
        rows2 = await page.evaluate('document.querySelectorAll("table tbody tr").length - 1')
        print(f'第2页: {rows2} 条')
        
        # 有数据就打印第1条标题
        if rows2 > 0:
            first = await page.evaluate('document.querySelector("table tbody tr:nth-child(2) td:nth-child(1) a")?.textContent?.trim()')
            print(f'第1条: {first}')
        
        await browser.close()

asyncio.run(main())
