a
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env python3
|
||||
"""测试通过点击分页按钮翻页"""
|
||||
from playwright.sync_api import sync_playwright
|
||||
from bs4 import BeautifulSoup
|
||||
import re
|
||||
|
||||
def test_click_pagination(menu_id=7):
|
||||
with sync_playwright() as p:
|
||||
browser = p.chromium.launch(headless=True)
|
||||
page = browser.new_page(viewport={'width': 1920, 'height': 1080})
|
||||
|
||||
url = f"https://niaodaifu.cn/health-information?menu_id={menu_id}"
|
||||
print(f"[打开] {url}")
|
||||
page.goto(url, wait_until='networkidle', timeout=30000)
|
||||
page.wait_for_timeout(3000)
|
||||
|
||||
def get_article_ids():
|
||||
soup = BeautifulSoup(page.content(), 'html.parser')
|
||||
ids = []
|
||||
for m in soup.find_all('div', class_='media'):
|
||||
a = m.find('a', class_='media-heading')
|
||||
if a:
|
||||
href = a.get('href', '')
|
||||
match = re.search(r'article_id=(\d+)', href)
|
||||
if match:
|
||||
ids.append(match.group(1))
|
||||
return ids
|
||||
|
||||
ids1 = get_article_ids()
|
||||
print(f"第1页 article_ids: {ids1}")
|
||||
|
||||
# 查找下一页按钮
|
||||
print("\n[分页按钮信息]")
|
||||
next_btn = page.query_selector('button.btn-next')
|
||||
if next_btn:
|
||||
print(f" 找到下一页按钮: {next_btn.get_attribute('class')}")
|
||||
print(f" 是否禁用: {next_btn.get_attribute('disabled')}")
|
||||
else:
|
||||
print(" 未找到 button.btn-next")
|
||||
|
||||
# 查找所有页码按钮
|
||||
pager_btns = page.query_selector_all('.el-pager li')
|
||||
print(f" 页码按钮数量: {len(pager_btns)}")
|
||||
for btn in pager_btns:
|
||||
print(f" - class={btn.get_attribute('class')} text={btn.text_content()}")
|
||||
|
||||
# 点击第2页
|
||||
if pager_btns and len(pager_btns) >= 2:
|
||||
print("\n[点击第2页]")
|
||||
pager_btns[1].click()
|
||||
page.wait_for_timeout(3000)
|
||||
ids2 = get_article_ids()
|
||||
print(f"第2页 article_ids: {ids2}")
|
||||
if ids2 != ids1:
|
||||
print("*** 点击分页成功!内容不同 ***")
|
||||
else:
|
||||
print("!!! 点击后内容未变 !!!")
|
||||
elif next_btn:
|
||||
print("\n[点击下一页按钮]")
|
||||
next_btn.click()
|
||||
page.wait_for_timeout(3000)
|
||||
ids2 = get_article_ids()
|
||||
print(f"第2页 article_ids: {ids2}")
|
||||
if ids2 != ids1:
|
||||
print("*** 点击下一页成功!内容不同 ***")
|
||||
else:
|
||||
print("!!! 点击后内容未变 !!!")
|
||||
|
||||
browser.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_click_pagination()
|
||||
Reference in New Issue
Block a user