Spaces:
Paused
Paused
feat(browser): add iframe keepalive click helper
Browse filesFiles changed (2)
2 unmodified lines345678987 unmodified lines9798991001011021032 unmodified linesfrom playwright.sync_api import Page, expectfrom utils.paths import logs_dirfrom utils.common import ensure_dirfrom browser.ws_helper import reconnect_ws, get_ws_status, dismiss_interaction_modalclass KeepAliveError(Exception):pass87 unmodified lines# 检测并关闭interaction-modal遮罩层(如果出现)dismiss_interaction_modal(page, logger)page.click('body')click_counter += 1# 检查WS状态是否发生变化2 unmodified lines345678987 unmodified lines9798991001011021031042 unmodified linesfrom playwright.sync_api import Page, expectfrom utils.paths import logs_dirfrom utils.common import ensure_dirfrom browser.ws_helper import reconnect_ws, get_ws_status, dismiss_interaction_modal, click_in_iframeclass KeepAliveError(Exception):pass87 unmodified lines# 检测并关闭interaction-modal遮罩层(如果出现)dismiss_interaction_modal(page, logger)# 在iframe内随机移动并点击保活click_in_iframe(page, logger)click_counter += 1# 检查WS状态是否发生变化
190 unmodified lines191192193190 unmodified linesif logger:logger.debug(f"关闭 interaction-modal 时出错: {e}")return False190 unmodified lines191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241190 unmodified linesif logger:logger.debug(f"关闭 interaction-modal 时出错: {e}")return Falsedef click_in_iframe(page: Page, logger=None) -> bool:"""在 iframe 内随机移动鼠标并点击一次,用于保活。避开顶部(状态栏和按钮区域)和右侧区域。返回: True 如果成功点击,False 如果失败"""try:iframe = page.locator('iframe[title="Preview"]')if iframe.count() == 0:return Falseiframe_box = iframe.first.bounding_box()if not iframe_box:return False# 安全区域:避开顶部80像素(状态栏+按钮)和右侧200像素(按钮区域)safe_left = iframe_box['x'] + 50safe_right = iframe_box['x'] + iframe_box['width'] - 200safe_top = iframe_box['y'] + 80safe_bottom = iframe_box['y'] + iframe_box['height'] - 50# 确保安全区域有效if safe_right <= safe_left or safe_bottom <= safe_top:return False# 随机起点(在安全区域内)curr_x = random.randint(int(safe_left), int(safe_right))curr_y = random.randint(int(safe_top), int(safe_bottom))# 随机移动几步(保持在安全区域内)for _ in range(random.randint(3, 6)):delta_x = random.randint(-30, 30)delta_y = random.randint(-20, 20)curr_x = max(int(safe_left), min(int(safe_right), curr_x + delta_x))curr_y = max(int(safe_top), min(int(safe_bottom), curr_y + delta_y))page.mouse.move(curr_x, curr_y)time.sleep(0.05)# 点击当前位置page.mouse.click(curr_x, curr_y)return Trueexcept Exception as e:if logger:logger.debug(f"在 iframe 内点击失败: {e}")return False
browser/navigation.py+3-2
browser/ws_helper.py+48