| 12345678910111213141516171819202122232425262728293031323334 |
- """步骤:登录。"""
- from __future__ import annotations
- from playwright.sync_api import Page, TimeoutError as PlaywrightTimeoutError
- from collectors.douyin.common import human_pause
- def click_login_if_needed(page: Page) -> None:
- login = page.get_by_role("button", name="登录").or_(page.get_by_text("登录", exact=True)).first
- try:
- if login.is_visible(timeout=3000):
- human_pause(0.8, 1.5)
- login.click()
- print("已点击登录按钮,请在浏览器中完成人工登录。")
- else:
- print("未看到登录按钮,可能已登录。")
- except PlaywrightTimeoutError:
- print("未看到登录按钮,可能已登录。")
- def wait_until_logged_in(page: Page, timeout_ms: int = 300_000) -> None:
- print("等待人工登录完成(最多 5 分钟)...")
- page.wait_for_function(
- """() => {
- const texts = Array.from(document.querySelectorAll('button, a, span, div'))
- .map(el => (el.innerText || '').trim())
- .filter(Boolean);
- return !texts.some(t => t === '登录');
- }""",
- timeout=timeout_ms,
- )
- print("检测到登录态已就绪。")
|