1212 VEO_SETTINGS_NUM_RESULTS_INPUT_SELECTOR , VEO_SETTINGS_ASPECT_RATIO_BUTTON_SELECTOR ,
1313 VEO_SETTINGS_DURATION_DROPDOWN_SELECTOR , VEO_SETTINGS_NEGATIVE_PROMPT_SELECTOR
1414)
15+ from browser .operations import safe_click
1516from .models import VideoGenerationConfig , GeneratedVideo
1617from models import ClientDisconnectedError
1718
@@ -27,31 +28,6 @@ async def _check_disconnect(self, check_client_disconnected: Callable, stage: st
2728 if check_client_disconnected (stage ):
2829 raise ClientDisconnectedError (f'[{ self .req_id } ] Client disconnected at stage: { stage } ' )
2930
30- async def _safe_click (self , locator : Locator , element_name : str , timeout : int = 2000 ) -> bool :
31- try :
32- await locator .wait_for (state = 'visible' , timeout = timeout )
33- except Exception as e :
34- self .logger .warning (f"[{ self .req_id } ] '{ element_name } ' 元素不可见: { e } " )
35- return False
36- try :
37- await locator .click (timeout = 500 )
38- return True
39- except Exception :
40- pass
41- await asyncio .sleep (0.1 )
42- try :
43- await locator .click (timeout = 500 , force = True )
44- return True
45- except Exception :
46- pass
47- await asyncio .sleep (0.1 )
48- try :
49- await locator .evaluate ('element => element.click()' )
50- return True
51- except Exception as e :
52- self .logger .error (f"[{ self .req_id } ] ❌ 所有点击 '{ element_name } ' 的尝试都失败了: { e } " )
53- return False
54-
5531
5632 async def navigate_to_veo_page (self , model : str , check_client_disconnected : Callable ):
5733 if model not in VEO_SUPPORTED_MODELS :
@@ -102,7 +78,7 @@ async def set_aspect_ratio(self, aspect_ratio: str, check_client_disconnected: C
10278 try :
10379 btn = self .page .locator (f'{ VEO_SETTINGS_ASPECT_RATIO_BUTTON_SELECTOR } :has-text("{ aspect_ratio } ")' )
10480 if await btn .count () > 0 :
105- if await self . _safe_click (btn .first , f'宽高比按钮 { aspect_ratio } ' ):
81+ if await safe_click (btn .first , f'宽高比按钮 { aspect_ratio } ' , self . req_id ):
10682 self .logger .info (f'[{ self .req_id } ] ✅ 宽高比已设置: { aspect_ratio } ' )
10783 return
10884 else :
@@ -124,12 +100,12 @@ async def set_duration(self, duration_seconds: int, check_client_disconnected: C
124100 if await dropdown .count () == 0 :
125101 self .logger .warning (f'[{ self .req_id } ] 未找到时长下拉框' )
126102 return
127- if not await self . _safe_click (dropdown , '时长下拉框' ):
103+ if not await safe_click (dropdown , '时长下拉框' , self . req_id ):
128104 continue
129105 await asyncio .sleep (0.3 )
130106 option = self .page .locator (f'mat-option:has-text("{ duration_seconds } ")' )
131107 if await option .count () > 0 :
132- if await self . _safe_click (option .first , f'时长选项 { duration_seconds } s' ):
108+ if await safe_click (option .first , f'时长选项 { duration_seconds } s' , self . req_id ):
133109 self .logger .info (f'[{ self .req_id } ] ✅ 视频时长已设置: { duration_seconds } s' )
134110 return
135111 else :
@@ -174,7 +150,7 @@ async def upload_image(self, image_bytes: bytes, mime_type: str, check_client_di
174150 if await add_media_btn .count () == 0 :
175151 self .logger .warning (f'[{ self .req_id } ] 未找到添加媒体按钮' )
176152 return
177- if not await self . _safe_click (add_media_btn , '添加媒体按钮' ):
153+ if not await safe_click (add_media_btn , '添加媒体按钮' , self . req_id ):
178154 if attempt < max_retries :
179155 continue
180156 return
@@ -209,7 +185,7 @@ async def fill_prompt(self, prompt: str, check_client_disconnected: Callable):
209185 await self .page .keyboard .press ('Escape' )
210186 await asyncio .sleep (0.3 )
211187 text_input = self .page .locator (VEO_PROMPT_INPUT_SELECTOR )
212- await self . _safe_click (text_input , '输入框' )
188+ await safe_click (text_input , '输入框' , self . req_id )
213189 await text_input .fill (prompt )
214190 await asyncio .sleep (0.2 )
215191 self .logger .info (f'[{ self .req_id } ] ✅ 提示词已填充' )
@@ -232,7 +208,7 @@ async def run_generation(self, check_client_disconnected: Callable):
232208 run_btn = self .page .locator (VEO_RUN_BUTTON_SELECTOR )
233209 await expect_async (run_btn ).to_be_visible (timeout = 5000 )
234210 await expect_async (run_btn ).to_be_enabled (timeout = 5000 )
235- if not await self . _safe_click (run_btn , 'Run 按钮' ):
211+ if not await safe_click (run_btn , 'Run 按钮' , self . req_id ):
236212 if attempt < max_retries :
237213 continue
238214 raise Exception ('Run 按钮点击失败' )
0 commit comments