hkfires commited on
Commit
cb05669
·
verified ·
1 Parent(s): 2ceecec

feat(browser): add iframe keepalive click helper

Browse files
Files changed (2) hide show
  1. browser/navigation.py +3 -2
  2. browser/ws_helper.py +48 -0
browser/navigation.py CHANGED
@@ -3,7 +3,7 @@ import os
3
  from playwright.sync_api import Page, expect
4
  from utils.paths import logs_dir
5
  from utils.common import ensure_dir
6
- from browser.ws_helper import reconnect_ws, get_ws_status, dismiss_interaction_modal
7
 
8
  class KeepAliveError(Exception):
9
  pass
@@ -97,7 +97,8 @@ def handle_successful_navigation(page: Page, logger, cookie_file_config, shutdow
97
  # 检测并关闭interaction-modal遮罩层(如果出现)
98
  dismiss_interaction_modal(page, logger)
99
 
100
- page.click('body')
 
101
  click_counter += 1
102
 
103
  # 检查WS状态是否发生变化
 
3
  from playwright.sync_api import Page, expect
4
  from utils.paths import logs_dir
5
  from utils.common import ensure_dir
6
+ from browser.ws_helper import reconnect_ws, get_ws_status, dismiss_interaction_modal, click_in_iframe
7
 
8
  class KeepAliveError(Exception):
9
  pass
 
97
  # 检测并关闭interaction-modal遮罩层(如果出现)
98
  dismiss_interaction_modal(page, logger)
99
 
100
+ # 在iframe内随机移动并点击保活
101
+ click_in_iframe(page, logger)
102
  click_counter += 1
103
 
104
  # 检查WS状态是否发生变化
browser/ws_helper.py CHANGED
@@ -191,3 +191,51 @@ def dismiss_interaction_modal(page: Page, logger=None) -> bool:
191
  if logger:
192
  logger.debug(f"关闭 interaction-modal 时出错: {e}")
193
  return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  if logger:
192
  logger.debug(f"关闭 interaction-modal 时出错: {e}")
193
  return False
194
+
195
+
196
+ def click_in_iframe(page: Page, logger=None) -> bool:
197
+ """
198
+ 在 iframe 内随机移动鼠标并点击一次,用于保活。
199
+ 避开顶部(状态栏和按钮区域)和右侧区域。
200
+
201
+ 返回: True 如果成功点击,False 如果失败
202
+ """
203
+ try:
204
+ iframe = page.locator('iframe[title="Preview"]')
205
+ if iframe.count() == 0:
206
+ return False
207
+
208
+ iframe_box = iframe.first.bounding_box()
209
+ if not iframe_box:
210
+ return False
211
+
212
+ # 安全区域:避开顶部80像素(状态栏+按钮)和右侧200像素(按钮区域)
213
+ safe_left = iframe_box['x'] + 50
214
+ safe_right = iframe_box['x'] + iframe_box['width'] - 200
215
+ safe_top = iframe_box['y'] + 80
216
+ safe_bottom = iframe_box['y'] + iframe_box['height'] - 50
217
+
218
+ # 确保安全区域有效
219
+ if safe_right <= safe_left or safe_bottom <= safe_top:
220
+ return False
221
+
222
+ # 随机起点(在安全区域内)
223
+ curr_x = random.randint(int(safe_left), int(safe_right))
224
+ curr_y = random.randint(int(safe_top), int(safe_bottom))
225
+
226
+ # 随机移动几步(保持在安全区域内)
227
+ for _ in range(random.randint(3, 6)):
228
+ delta_x = random.randint(-30, 30)
229
+ delta_y = random.randint(-20, 20)
230
+ curr_x = max(int(safe_left), min(int(safe_right), curr_x + delta_x))
231
+ curr_y = max(int(safe_top), min(int(safe_bottom), curr_y + delta_y))
232
+ page.mouse.move(curr_x, curr_y)
233
+ time.sleep(0.05)
234
+
235
+ # 点击当前位置
236
+ page.mouse.click(curr_x, curr_y)
237
+ return True
238
+ except Exception as e:
239
+ if logger:
240
+ logger.debug(f"在 iframe 内点击失败: {e}")
241
+ return False