From 47d9140dc6a3a1071ef361231d0d8330f17b3dc5 Mon Sep 17 00:00:00 2001 From: Misaka Date: Sun, 21 Jun 2026 10:55:56 +0800 Subject: [PATCH] Add Baishi tab cleanup helper and close tab after extraction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add _close_tab(page, tab_name) for Baishi's tab close affordance (li>span text + i[title='关闭标签页']), matching the other sites - Close the 扫描综合查询 tab both on the empty-data early return and after a successful download; return True on completion Co-Authored-By: Claude --- site_baishi.py | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/site_baishi.py b/site_baishi.py index 872494a..e202da0 100644 --- a/site_baishi.py +++ b/site_baishi.py @@ -7,6 +7,25 @@ from playwright.sync_api import sync_playwright from paths import DOWNLOAD_DIR, CONFIG_PATH +def _close_tab(page, tab_name): + """关闭指定名称的百世标签页。 + + 通过 li > span 中的文本定位标签,并点击其内部 title 为 "关闭标签页" 的图标。 + """ + try: + tab = page.locator("li").filter(has=page.locator("span", has_text=tab_name)) + if tab.count() == 0: + print(f" ℹ️ 未找到标签页【{tab_name}】(可能尚未打开或已关闭),跳过。") + return + + # 点击百世特有的关闭按钮 + tab.first.locator("i[title='关闭标签页']").click() + print(f" 🗙 已关闭标签页【{tab_name}】") + page.wait_for_timeout(300) + except Exception as e: + print(f" ⚠️ 关闭标签页【{tab_name}】时出错: {e}") + + def baishi_download_undelivered_data(page): """百世:一键提取应到未到(当日未扫)数据""" print("\n▶ 开始执行【百世 - 一键提取应到未到数据】任务...") @@ -21,8 +40,7 @@ def baishi_download_undelivered_data(page): # 打开基础服务菜单面板 page.locator("div.nav-level1", has_text="基础服务").click() - # 限制在菜单面板(nav-level2-wrapper)内查找, - # 避免命中右侧同名标签页 + # 限制在菜单面板(nav-level2-wrapper)内查找,避免命中右侧同名标签页 page.locator(".nav-level2-wrapper").locator( "a", has_text="扫描综合查询" ).click() @@ -39,7 +57,11 @@ def baishi_download_undelivered_data(page): # 特殊情况处理:检查未扫数量是否为0 cell_text = target_cell.inner_text().strip() if cell_text == "0" or cell_text == "": - print(f" ℹ️ 注意:当日未扫数量为【{cell_text}】,无数据需要提取,任务结束。") + print( + f" ℹ️ 注意:当日未扫数量为【{cell_text}】,无数据需要提取,任务结束。" + ) + # 数据为空时,提前结束前也需清理环境 + _close_tab(page, "扫描综合查询") return # 有未扫数据,继续点击操作 @@ -97,7 +119,12 @@ def baishi_download_undelivered_data(page): print(f" 已下载: {save_path}") print(f"====================================================") + # 6. 环境清理 + print(">> 任务完成,正在清理环境...") + _close_tab(page, "扫描综合查询") + print("\n【百世 - 应到未到数据提取】流程结束。") + return True except Exception as e: print(f"\n❌ 任务执行过程中发生异常: {e}")