Skip to content

Commit 94d9da1

Browse files
authored
Merge pull request #280 from NikkeTryHard/main
fix: Update selectors for Google AI Studio UI changes and fix test mocks (#278)
2 parents 0348f44 + e10d8bf commit 94d9da1

8 files changed

Lines changed: 2033 additions & 1388 deletions

File tree

browser_utils/initialization/core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,11 @@ async def initialize_page_logic(
293293
await found_page.bring_to_front()
294294

295295
try:
296-
input_wrapper_locator = found_page.locator("ms-prompt-input-wrapper")
296+
input_wrapper_locator = found_page.locator(
297+
"ms-prompt-box .prompt-box-container"
298+
)
299+
if await input_wrapper_locator.count() == 0:
300+
input_wrapper_locator = found_page.locator("ms-prompt-box")
297301
await expect_async(input_wrapper_locator).to_be_visible(timeout=35000)
298302
await expect_async(found_page.locator(INPUT_SELECTOR)).to_be_visible(
299303
timeout=10000

browser_utils/operations_modules/interactions.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ async def get_response_via_edit_button(
8383
edit_button = last_message_container.get_by_label("Edit")
8484
finish_edit_button = last_message_container.get_by_label("Stop editing")
8585
autosize_textarea_locator = last_message_container.locator("ms-autosize-textarea")
86-
actual_textarea_locator = autosize_textarea_locator.locator("textarea")
86+
actual_textarea_locator = last_message_container.locator("textarea")
8787

8888
try:
8989
logger.info(" - 尝试悬停最后一条消息以显示 'Edit' 按钮...")
@@ -128,30 +128,38 @@ async def get_response_via_edit_button(
128128
textarea_failed = False
129129

130130
try:
131-
await expect_async(autosize_textarea_locator).to_be_visible(
132-
timeout=CLICK_TIMEOUT_MS
133-
)
134-
check_client_disconnected("编辑响应 - autosize-textarea 可见后: ")
131+
target_locator = autosize_textarea_locator
132+
if await target_locator.count() == 0:
133+
target_locator = actual_textarea_locator
135134

136-
try:
137-
data_value_content = await autosize_textarea_locator.get_attribute(
138-
"data-value"
139-
)
140-
check_client_disconnected("编辑响应 - get_attribute data-value 后: ")
141-
if data_value_content is not None:
142-
response_content = str(data_value_content)
143-
logger.info(" - 从 data-value 获取内容成功。")
144-
except asyncio.CancelledError:
145-
raise
146-
except Exception as data_val_err:
147-
logger.warning(f" - 获取 data-value 失败: {data_val_err}")
148-
check_client_disconnected(
149-
"编辑响应 - get_attribute data-value 错误后: "
150-
)
135+
if await target_locator.count() == 0:
136+
raise RuntimeError("未找到可编辑的文本区域")
137+
138+
await expect_async(target_locator).to_be_visible(timeout=CLICK_TIMEOUT_MS)
139+
check_client_disconnected("编辑响应 - 文本区域可见后: ")
140+
141+
if await autosize_textarea_locator.count() > 0 and response_content is None:
142+
try:
143+
data_value_content = await autosize_textarea_locator.get_attribute(
144+
"data-value"
145+
)
146+
check_client_disconnected(
147+
"编辑响应 - get_attribute data-value 后: "
148+
)
149+
if data_value_content is not None:
150+
response_content = str(data_value_content)
151+
logger.info(" - 从 data-value 获取内容成功。")
152+
except asyncio.CancelledError:
153+
raise
154+
except Exception as data_val_err:
155+
logger.warning(f" - 获取 data-value 失败: {data_val_err}")
156+
check_client_disconnected(
157+
"编辑响应 - get_attribute data-value 错误后: "
158+
)
151159

152-
if response_content is None:
160+
if response_content is None and await actual_textarea_locator.count() > 0:
153161
logger.info(
154-
" - data-value 获取失败或为None,尝试从内部 textarea 获取 input_value..."
162+
" - data-value 获取失败或不存在,尝试从 textarea 获取 input_value..."
155163
)
156164
try:
157165
await expect_async(actual_textarea_locator).to_be_visible(

browser_utils/page_controller_modules/input.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
PROMPT_TEXTAREA_SELECTOR,
1212
RESPONSE_CONTAINER_SELECTOR,
1313
SUBMIT_BUTTON_SELECTOR,
14+
UPLOAD_BUTTON_SELECTOR,
1415
)
1516
from logging_utils import set_request_id
1617
from models import ClientDisconnectedError
@@ -28,8 +29,9 @@ async def submit_prompt(
2829
set_request_id(self.req_id)
2930
self.logger.info(f"填充并提交提示 ({len(prompt)} chars)...")
3031
prompt_textarea_locator = self.page.locator(PROMPT_TEXTAREA_SELECTOR)
31-
autosize_wrapper_locator = self.page.locator(
32-
"ms-prompt-input-wrapper ms-autosize-textarea"
32+
autosize_wrapper_locator = self.page.locator("ms-prompt-box .text-wrapper")
33+
legacy_autosize_wrapper = self.page.locator(
34+
"ms-prompt-box ms-autosize-textarea"
3335
)
3436
submit_button_locator = self.page.locator(SUBMIT_BUTTON_SELECTOR)
3537

@@ -50,10 +52,19 @@ async def submit_prompt(
5052
""",
5153
prompt,
5254
)
53-
await autosize_wrapper_locator.evaluate(
54-
'(element, text) => { element.setAttribute("data-value", text); }',
55-
prompt,
56-
)
55+
autosize_target = autosize_wrapper_locator
56+
if await autosize_target.count() == 0:
57+
autosize_target = legacy_autosize_wrapper
58+
if await autosize_target.count() > 0:
59+
try:
60+
await autosize_target.first.evaluate(
61+
'(element, text) => { element.setAttribute("data-value", text); }',
62+
prompt,
63+
)
64+
except Exception as autosize_err:
65+
self.logger.debug(
66+
f" autosize wrapper update skipped: {autosize_err}"
67+
)
5768
await self._check_disconnect(check_client_disconnected, "After Input Fill")
5869

5970
# 上传(仅使用菜单 + 隐藏 input 设置文件;处理可能的授权弹窗)
@@ -162,9 +173,8 @@ async def _open_upload_menu_and_choose_file(self, files_list: List[str]) -> bool
162173
except Exception:
163174
pass
164175

165-
trigger = self.page.locator(
166-
'button[aria-label="Insert assets such as images, videos, files, or audio"]'
167-
)
176+
trigger = self.page.locator(UPLOAD_BUTTON_SELECTOR).first
177+
await expect_async(trigger).to_be_visible(timeout=3000)
168178
await trigger.click()
169179
menu_container = self.page.locator("div.cdk-overlay-container")
170180
# 等待菜单显示
@@ -373,9 +383,9 @@ async def _simulate_drag_drop_files(
373383

374384
candidates = [
375385
target_locator,
376-
self.page.locator("ms-prompt-input-wrapper ms-autosize-textarea textarea"),
377-
self.page.locator("ms-prompt-input-wrapper ms-autosize-textarea"),
378-
self.page.locator("ms-prompt-input-wrapper"),
386+
self.page.locator(PROMPT_TEXTAREA_SELECTOR),
387+
self.page.locator("ms-prompt-box .text-wrapper"),
388+
self.page.locator("ms-prompt-box"),
379389
]
380390

381391
last_err = None

config/selectors.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,32 @@
44
"""
55

66
# --- 输入相关选择器 ---
7-
PROMPT_TEXTAREA_SELECTOR = "ms-prompt-input-wrapper ms-autosize-textarea textarea"
7+
# 主输入 textarea 同时兼容 aria-label 及普通 textarea,防止结构再调整
8+
PROMPT_TEXTAREA_SELECTOR = (
9+
"ms-prompt-box ms-autosize-textarea textarea, "
10+
'ms-prompt-box textarea[aria-label="Enter a prompt"], '
11+
"ms-prompt-box textarea"
12+
)
813
INPUT_SELECTOR = PROMPT_TEXTAREA_SELECTOR
914
INPUT_SELECTOR2 = PROMPT_TEXTAREA_SELECTOR
1015

1116
# --- 按钮选择器 ---
12-
# 发送按钮:优先匹配 aria-label="Run" 的按钮;如页面结构变更,可退化到容器内的提交按钮。
13-
SUBMIT_BUTTON_SELECTOR = 'button[aria-label="Run"].run-button, ms-run-button button[type="submit"].run-button'
17+
# 发送按钮:优先匹配 prompt 区域内 aria-label="Run" 的提交按钮
18+
SUBMIT_BUTTON_SELECTOR = (
19+
'ms-prompt-box ms-run-button button[aria-label="Run"], '
20+
'ms-prompt-box button[aria-label="Run"][type="submit"], '
21+
'button[aria-label="Run"].run-button, '
22+
'ms-run-button button[type="submit"].run-button'
23+
)
1424
CLEAR_CHAT_BUTTON_SELECTOR = 'button[data-test-clear="outside"][aria-label="New chat"], button[aria-label="New chat"]'
1525
CLEAR_CHAT_CONFIRM_BUTTON_SELECTOR = (
1626
'button.ms-button-primary:has-text("Discard and continue")'
1727
)
18-
UPLOAD_BUTTON_SELECTOR = 'button[aria-label^="Insert assets"]'
28+
UPLOAD_BUTTON_SELECTOR = (
29+
'button[data-test-id="add-media-button"], '
30+
'button[aria-label^="Insert assets"], '
31+
'button[aria-label^="Insert images"]'
32+
)
1933

2034
# --- 响应相关选择器 ---
2135
RESPONSE_CONTAINER_SELECTOR = "ms-chat-turn .chat-turn-container.model"
@@ -32,7 +46,9 @@
3246
EDIT_MESSAGE_BUTTON_SELECTOR = (
3347
"ms-chat-turn:last-child .actions-container button.toggle-edit-button"
3448
)
35-
MESSAGE_TEXTAREA_SELECTOR = "ms-chat-turn:last-child ms-text-chunk ms-autosize-textarea"
49+
MESSAGE_TEXTAREA_SELECTOR = (
50+
"ms-chat-turn:last-child textarea, ms-chat-turn:last-child ms-text-chunk textarea"
51+
)
3652
FINISH_EDIT_BUTTON_SELECTOR = 'ms-chat-turn:last-child .actions-container button.toggle-edit-button[aria-label="Stop editing"]'
3753

3854
# --- 菜单和复制相关选择器 ---

0 commit comments

Comments
 (0)