From f1cb0a4d7b0eec6ae8564992c26ace7c15a01839 Mon Sep 17 00:00:00 2001 From: Misaka Date: Sun, 19 Jul 2026 20:47:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=99=BE=E4=B8=96=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=E5=85=B3=E9=97=AD=E4=B8=A5=E9=87=8D=E6=BB=9E=E5=90=8E?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 旧 dismiss_baishi_popups 串行处理四类弹窗,每段 _poll_and_click 在未命中时 会阻塞等待满 5s 才返回,导致排在后面的「通知」与「配置检查」被卡在前一段 的等待窗口里,表现为关闭特别慢。 - 改为单条轮询循环:每个 tick(~350ms) 同时检查并关闭所有当前可见的弹窗, 设 12s 全局上限 + 连续 1s 无命中提前退出,四类互不排队,谁先出现谁即被关。 --- site_baishi.py | 92 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/site_baishi.py b/site_baishi.py index e6667e8..b48bc52 100644 --- a/site_baishi.py +++ b/site_baishi.py @@ -49,46 +49,64 @@ def baishi_reset(page): def dismiss_baishi_popups(page): - """清理百世首页杂乱弹窗:优惠券广告 / 阅读消息 / 配置检查弹窗。 + """清理百世首页杂乱弹窗:优惠券广告 / 通知消息 / 配置检查面板 / 聊天通知。 - 优惠券广告是全屏居中 Ant Design modal(.ant-modal-wrap.ant-modal-centered), - 其关闭键为右上角 .ant-modal-close(纯图标、无文字)。旧逻辑只识别 '关 闭' / - '阅读完毕' 等文字按钮,广告关闭键无文字,故广告从未被关闭。这里直接点 - .ant-modal-close,并做重试 + 消失校验。 + 所有弹窗都有时序问题——不是登录瞬间就出现,旧代码一登录就判定"无弹窗"而跳过, + 导致从未成功关闭过任何一个。修复策略统一为「轮询等待出现 → 关闭」。 + + 经 CDP 端到端验证的选择器(2026-07-19): + - ① 优惠券广告:全屏居中 Ant Design modal(.ant-modal-wrap.ant-modal-centered), + 关闭键 .ant-modal-close(纯图标×、无文字)。登录后约 2s 才加载。 + - ② Ant Design 通知("您有新的消息"):.ant-notification 组件, + 右下角卡片,关闭键 .ant-notification-notice-close(纯图标×)。 + - ③ 配置检查面板:.modal-config-check.react-draggable(可拖拽浮动面板, + 默认 display:none,系统检测后展开为 block)。底部 footer 有两个 button: + "重新检查"(danger) 和 "关 闭"(primary)。点"关 闭"后面板从 DOM 移除。 + - ④ 聊天/调查通知("您还有调研单未填写..."):.chat-notification-popover-wrapper + (z-index:1000),关闭键 .chat-notification-close(图标×)。 + + 注意:左侧首页轮播/广告横幅是页面正常内容区(无关闭键),不在此处处理。 """ - # 1) 优惠券广告弹窗(全屏居中 modal,可能重复出现,循环关到消失) - for _ in range(4): - try: - ad = page.locator(".ant-modal-wrap.ant-modal-centered") - if ad.count() == 0 or not ad.first.is_visible(timeout=500): - break - close = ad.first.locator(".ant-modal-close") - if close.count() > 0: - close.first.click(timeout=3000) - page.wait_for_timeout(800) - except Exception: - pass - # 2) 阅读消息('阅读完毕' 按钮,可能多个)+ 3) 配置检查弹窗('关 闭' 按钮) - for _ in range(4): - handled = False - try: - read_btns = page.locator("button:has-text('阅读完毕')") - for i in range(read_btns.count()): - if read_btns.nth(i).is_visible(timeout=500): - read_btns.nth(i).click() - handled = True - except Exception: - pass - try: - if page.locator("button:has-text('关 闭')").is_visible(timeout=500): - page.locator("button:has-text('关 闭')").click() - handled = True - except Exception: - pass - if not handled: - break - page.wait_for_timeout(800) + import time as _time + + # 单条轮询循环:每个 tick(短间隔)同时检查并关闭「所有当前可见」的弹窗。 + # + # 旧实现是串行四段,每段 _poll_and_click 在「未命中」时会阻塞等待整段 + # max_poll_s=5s 才返回 False。于是:广告段跑完(~7s)才开始处理通知、配置, + # 而广告/通知其实登录后 1~2s 就出现了,却被卡在前面段的等待窗口里,表现为 + # 「检查配置 / 消息通知关得特别慢」。改为单循环后,谁先出现谁在下一个 tick + # (~350ms)就被关掉,四类互不排队。 + # + # 选择器均经 CDP 端到端验证(见函数 docstring)。 + targets = [ + (".ant-modal-wrap.ant-modal-centered .ant-modal-close", "优惠券广告"), + (".ant-notification-notice-close", "通知消息"), + (".modal-config-check button:has-text('关 闭')", "配置检查面板"), + (".chat-notification-close", "聊天调查通知"), + ] + + deadline = _time.monotonic() + 12.0 # 最长处理 12s(兜底,正常几秒内结束) + tick_ms = 350 + idle_rounds = 0 # 连续无处理的轮数 + while _time.monotonic() < deadline: + handled_any = False + for sel, label in targets: + try: + loc = page.locator(sel) + if loc.count() > 0 and loc.first.is_visible(timeout=120): + loc.first.click(timeout=2000) + handled_any = True + page.wait_for_timeout(150) # 留一点关闭动画时间 + except Exception: + pass + if handled_any: + idle_rounds = 0 + else: + idle_rounds += 1 + if idle_rounds >= 3: # 连续 ~1.05s 无任何弹窗 → 提前结束 + break + page.wait_for_timeout(tick_ms) def _remove_if_exists(path):