#!/usr/bin/env python3
"""测试翻页修复"""
import asyncio
from playwright.async_api import async_playwright

async def test():
    async with async_playwright() as pw:
        browser = await pw.chromium.launch(headless=True, args=['--no-sandbox'])
        page = await browser.new_page()

        await page.goto('https://www.yanshougs.com/list/2.html', timeout=30000, wait_until='networkidle')
        await page.wait_for_timeout(5000)
        rows1 = await page.evaluate('document.querySelectorAll("table tbody tr").length - 1')
        print(f'第1页: {rows1} 条')

        for pn in range(2, 5):
            clicked = await page.evaluate(f'''() => {{
                var link = document.querySelector('a[href="/publicity_list?page={pn}"]');
                if (link) {{ link.click(); return true; }}
                var next = document.querySelector('.pagination a:last-child');
                if (next && (next.textContent.includes('下一页') || next.textContent.includes('>'))) {{
                    next.click(); return true;
                }}
                return false;
            }}''')
            if clicked:
                try:
                    await page.wait_for_load_state('networkidle', timeout=30000)
                except:
                    pass
                await page.wait_for_timeout(5000)
                rows = await page.evaluate('document.querySelectorAll("table tbody tr").length - 1')
                print(f'第{pn}页: {rows} 条 (翻页)')
            else:
                print(f'第{pn}页: 无链接')
                break

        await browser.close()

asyncio.run(test())
