Skip to content

Commit 9d7a410

Browse files
committed
健壮性优化
1 parent 947f962 commit 9d7a410

1 file changed

Lines changed: 41 additions & 1 deletion

File tree

browser_utils/page_controller.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,25 @@ async def _execute_chat_clear(self, clear_chat_button_locator, confirm_button_lo
604604
await confirm_button_locator.click(timeout=CLICK_TIMEOUT_MS)
605605
else:
606606
self.logger.info(f"[{self.req_id}] 点击\"清空聊天\"按钮: {CLEAR_CHAT_BUTTON_SELECTOR}")
607-
await clear_chat_button_locator.click(timeout=CLICK_TIMEOUT_MS)
607+
# 若存在透明遮罩层拦截指针事件,先尝试清理
608+
try:
609+
await self._dismiss_backdrops()
610+
except Exception:
611+
pass
612+
try:
613+
await clear_chat_button_locator.click(timeout=CLICK_TIMEOUT_MS)
614+
except Exception as first_click_err:
615+
# 尝试再次清理遮罩,并使用 force 点击作为兜底
616+
self.logger.warning(f"[{self.req_id}] 清空按钮第一次点击失败,尝试清理遮罩并强制点击: {first_click_err}")
617+
try:
618+
await self._dismiss_backdrops()
619+
except Exception:
620+
pass
621+
try:
622+
await clear_chat_button_locator.click(timeout=CLICK_TIMEOUT_MS, force=True)
623+
except Exception as force_click_err:
624+
self.logger.error(f"[{self.req_id}] 清空按钮强制点击仍失败: {force_click_err}")
625+
raise
608626
await self._check_disconnect(check_client_disconnected, "清空聊天 - 点击\"清空聊天\"后")
609627

610628
try:
@@ -656,6 +674,28 @@ async def _execute_chat_clear(self, clear_chat_button_locator, confirm_button_lo
656674

657675
await self._check_disconnect(check_client_disconnected, f"清空聊天 - 消失检查尝试 {attempt_disappear + 1} 后")
658676

677+
async def _dismiss_backdrops(self):
678+
"""尝试关闭可能残留的 cdk 透明遮罩层以避免点击被拦截。"""
679+
try:
680+
backdrop = self.page.locator('div.cdk-overlay-backdrop.cdk-overlay-backdrop-showing, div.cdk-overlay-backdrop.cdk-overlay-transparent-backdrop.cdk-overlay-backdrop-showing')
681+
for i in range(3):
682+
cnt = 0
683+
try:
684+
cnt = await backdrop.count()
685+
except Exception:
686+
cnt = 0
687+
if cnt and cnt > 0:
688+
self.logger.info(f"[{self.req_id}] 检测到透明遮罩层 ({cnt}),发送 ESC 以关闭 (尝试 {i+1}/3)。")
689+
try:
690+
await self.page.keyboard.press('Escape')
691+
await asyncio.sleep(0.2)
692+
except Exception:
693+
pass
694+
else:
695+
break
696+
except Exception:
697+
pass
698+
659699
async def _verify_chat_cleared(self, check_client_disconnected: Callable):
660700
"""验证聊天已清空"""
661701
last_response_container = self.page.locator(RESPONSE_CONTAINER_SELECTOR).last

0 commit comments

Comments
 (0)