Add multi-round popup cleanup engine for Baishi site initialization

- Loop up to 5 rounds to dismiss 阅读完毕 / 配置检查 / 通知提示 / 广告 popups until the page is clean
- Revert undelivered-data docstring to dynamic-prefix wording

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-06-16 22:20:07 +08:00
parent ec1a9d9fbc
commit 5c3225388e

View File

@@ -16,7 +16,7 @@ SITES_CONFIG = {
def task_process_undelivered_data(site_name="顺心"):
"""全局模块:应到未到异常件比对引擎 (本地离线比对)"""
"""全局模块:应到未到异常件比对引擎 (支持动态网点前缀)"""
print(f"\n▶ 开始执行【{site_name} - 应到未到数据处理】任务...")
download_dir = os.path.join(os.getcwd(), "downloads")
@@ -57,7 +57,7 @@ def task_process_undelivered_data(site_name="顺心"):
df_output.to_excel(output_path, index=False)
print(f"====================================================")
print(f" 🎉 异常比流完成!独立数据已安全输出。")
print(f" 🎉 异常比采取流完成!独立数据已安全输出。")
print(f" 📁 成果归档路径: {output_path}")
print(f"====================================================")
@@ -69,17 +69,14 @@ def run_multi_site_daemon():
"""多网点自动化主控引擎"""
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
# 取消禁用视口大小限制,防止有些系统自适应出问题
context = browser.new_context(viewport={"width": 1920, "height": 1080})
# 用于存储网点名称与 Page 对象的映射字典
pages_map = {}
print("\n====================================================")
print("【初始化阶段】正在构建多网点运行环境...")
print("====================================================")
# 遍历配置,依次创建独立的标签页
for site_name, url in SITES_CONFIG.items():
print(f">> 正在打开【{site_name}】页面: {url}")
page = context.new_page()
@@ -92,25 +89,22 @@ def run_multi_site_daemon():
print("请在弹出的浏览器中,依次切换标签页,人工完成所有网点的登录!")
print("====================================================")
# 阻塞程序,等待用户登录完毕
input(">> 登录全部完成后,请在此处按下【回车键】正式接管中控台...")
print("\n====================================================")
print("【系统接管】正在执行各网点就绪前初始化动作...")
print("====================================================")
# 处理【顺心】网点的登录后就绪判定与弹窗清理
# 顺心环境净化
try:
sx_page = pages_map["顺心"]
sx_page.bring_to_front()
print(">> 正在处理【顺心】网点初始状态...")
# 使用较长超时时间确认登录状态(避免回车按得太早没加载完)
sx_page.wait_for_selector('h1:has-text("盟商门户网")', timeout=30000)
print(" 🎉 登录成功!系统已接管顺心浏览器。")
sx_page.wait_for_timeout(1000)
# 跳过初始一些弹窗干扰
sx_page.locator("a").nth(4).click()
sx_page.wait_for_timeout(1000)
sx_page.get_by_role("button", name="Close").click()
@@ -118,18 +112,81 @@ def run_multi_site_daemon():
sx_page.get_by_role("button", name="不再询问").click()
sx_page.wait_for_timeout(1000)
print(" ✅ 【顺心】弹窗清理完毕,状态就绪!")
except Exception as e:
print(f" ⚠️ 【顺心】初始化异常 (无弹窗可忽略): {e}")
# 百世环境检测
except Exception as e:
print(f" ⚠️ 【顺心】初始化或弹窗清理异常 (如无弹窗可忽略): {e}")
# 百世环境检测与“打地鼠”清理引擎
try:
bs_page = pages_map["百世"]
bs_page.bring_to_front()
print(">> 正在处理【百世】网点初始状态...")
print("\n>> 正在处理【百世】网点初始状态...")
bs_page.wait_for_selector('h1[title="百世快运"]', timeout=30000)
print(" 🎉 登录成功!系统已接管百世浏览器。")
print(" ✅ 【百世】状态就绪!")
print(" >> 启动无序弹窗清理机制 (打地鼠模式)...")
# 留出一点时间让所有前端请求返回并渲染弹窗
bs_page.wait_for_timeout(2000)
# 最多循环 5 轮,每次尝试点掉视野内的所有垃圾弹窗
for round_idx in range(5):
handled_any = False
# 1. 清理【阅读完毕】(可能叠加出现多个)
try:
read_btns = bs_page.locator("button:has-text('阅读完毕')")
if read_btns.count() > 0:
for i in range(read_btns.count()):
if read_btns.nth(i).is_visible():
read_btns.nth(i).click()
bs_page.wait_for_timeout(500)
handled_any = True
print(" -> 已点击【阅读完毕】")
except Exception:
pass
# 2. 清理【配置检查 -> 关 闭】
try:
close_config = bs_page.locator("button:has-text('关 闭')")
if close_config.is_visible():
close_config.click()
bs_page.wait_for_timeout(500)
handled_any = True
print(" -> 已关闭【配置检查】")
except Exception:
pass
# 3. 清理【通知提示】
try:
notice_close = bs_page.locator("a.ant-notification-notice-close")
if notice_close.is_visible():
notice_close.click()
bs_page.wait_for_timeout(500)
handled_any = True
print(" -> 已关闭【通知提示】")
except Exception:
pass
# 4. 清理【广告】(捕获具有 close-circle 图标的 SVG)
try:
ad_close = bs_page.locator("svg[data-icon='close-circle']")
if ad_close.is_visible():
ad_close.click()
bs_page.wait_for_timeout(500)
handled_any = True
print(" -> 已关闭【广告弹窗】")
except Exception:
pass
# 如果这一轮没有任何可见的弹窗被处理,说明页面已经干净了,跳出循环
if not handled_any:
break
bs_page.wait_for_timeout(1000)
print(" ✅ 【百世】界面净化完毕,状态就绪!")
except Exception as e:
print(f" ⚠️ 【百世】初始化异常: {e}")