diff --git a/runtime.py b/runtime.py index 1bb5ade..1b8cd86 100644 --- a/runtime.py +++ b/runtime.py @@ -290,7 +290,10 @@ def launch_and_prepare(debug_mode=False, debug_target=""): # 3. 启动 Playwright(不用 with,改 .start(),由 RuntimeContext.stop() 收尾) pw = sync_playwright().start() - browser = pw.chromium.launch(headless=False) + # 调试模式临时开启 CDP 端口,便于外部 Playwright(如 Playwright CLI 技能) + # 通过 connectOverCDP 挂载到已登录的页面读取内容。仅调试模式生效,不影响服务模式。 + _launch_args = ["--remote-debugging-port=9223"] if debug_mode else [] + browser = pw.chromium.launch(headless=False, args=_launch_args) context = browser.new_context(viewport={"width": 1920, "height": 1080}) pages_map = {} @@ -399,29 +402,10 @@ def _dismiss_initial_popups(pages_map): try: bs_page = pages_map["百世"] bs_page.bring_to_front() - print(">> 正在处理【百世】阅读完毕与关闭按钮...") - for _ in range(4): - handled = False - 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(timeout=500): - read_btns.nth(i).click() - handled = True - except Exception: - pass - try: - if bs_page.locator("button:has-text('关 闭')").is_visible( - timeout=500 - ): - bs_page.locator("button:has-text('关 闭')").click() - handled = True - except Exception: - pass - if not handled: - break - bs_page.wait_for_timeout(800) + print(">> 正在处理【百世】初始弹窗(阅读消息 / 配置检查 / 优惠券广告)...") + # 委托给 site_baishi 的专用清理:优惠券广告是全屏居中 modal, + # 关闭键为 .ant-modal-close(纯图标无文字),必须点它才能真正关掉。 + site_baishi.dismiss_baishi_popups(bs_page) print(" ✅ 【百世】初始弹窗处理完成。") except Exception: pass diff --git a/site_baishi.py b/site_baishi.py index e657284..e6667e8 100644 --- a/site_baishi.py +++ b/site_baishi.py @@ -48,6 +48,49 @@ def baishi_reset(page): page.wait_for_timeout(1500) +def dismiss_baishi_popups(page): + """清理百世首页杂乱弹窗:优惠券广告 / 阅读消息 / 配置检查弹窗。 + + 优惠券广告是全屏居中 Ant Design modal(.ant-modal-wrap.ant-modal-centered), + 其关闭键为右上角 .ant-modal-close(纯图标、无文字)。旧逻辑只识别 '关 闭' / + '阅读完毕' 等文字按钮,广告关闭键无文字,故广告从未被关闭。这里直接点 + .ant-modal-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) + + def _remove_if_exists(path): """删除文件(若存在):流程开头清理上次的最终文件,避免无数据/失败时残留旧数据。""" try: