Skip to content

Commit ebeed2d

Browse files
fix: update upload button selector for new AI Studio UI (#290)
Google AI Studio changed the menu item text from 'Upload File' to 'Upload a file'. Added fallback chain to support both old and new UI versions.
1 parent ac5b234 commit ebeed2d

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

  • browser_utils/page_controller_modules

browser_utils/page_controller_modules/input.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,29 @@ async def _open_upload_menu_and_choose_file(self, files_list: List[str]) -> bool
203203
self.logger.warning(" 未能显示上传菜单面板。")
204204
return False
205205

206-
# 仅使用 aria-label='Upload File' 的菜单项
206+
# 使用 aria-label 或文本匹配 'Upload a file' / 'Upload File' 的菜单项
207207
try:
208+
# 优先匹配新 UI: "Upload a file"
208209
upload_btn = menu_container.locator(
209-
"div[role='menu'] button[role='menuitem'][aria-label='Upload File']"
210+
"div[role='menu'] button[role='menuitem'][aria-label='Upload a file']"
210211
)
211212
if await upload_btn.count() == 0:
212-
# 退化到按文本匹配 Upload File
213+
# 回退到旧 UI: "Upload File"
214+
upload_btn = menu_container.locator(
215+
"div[role='menu'] button[role='menuitem'][aria-label='Upload File']"
216+
)
217+
if await upload_btn.count() == 0:
218+
# 退化到按文本匹配 (新 UI)
219+
upload_btn = menu_container.locator(
220+
"div[role='menu'] button[role='menuitem']:has-text('Upload a file')"
221+
)
222+
if await upload_btn.count() == 0:
223+
# 退化到按文本匹配 (旧 UI)
213224
upload_btn = menu_container.locator(
214225
"div[role='menu'] button[role='menuitem']:has-text('Upload File')"
215226
)
216227
if await upload_btn.count() == 0:
217-
self.logger.warning(" 未找到 'Upload File' 菜单项。")
228+
self.logger.warning(" 未找到 'Upload a file' 或 'Upload File' 菜单项。")
218229
return False
219230
btn = upload_btn.first
220231
await expect_async(btn).to_be_visible(timeout=2000)
@@ -223,7 +234,7 @@ async def _open_upload_menu_and_choose_file(self, files_list: List[str]) -> bool
223234
if await input_loc.count() > 0:
224235
await input_loc.set_input_files(files_list)
225236
self.logger.info(
226-
f" 通过菜单项(Upload File) 隐藏 input 设置文件成功: {len(files_list)} 个"
237+
f" 通过菜单项(Upload a file) 隐藏 input 设置文件成功: {len(files_list)} 个"
227238
)
228239
else:
229240
# 回退为原生文件选择器

0 commit comments

Comments
 (0)