1212 MAT_CHIP_REMOVE_BUTTON_SELECTOR , TOP_P_INPUT_SELECTOR , SUBMIT_BUTTON_SELECTOR ,
1313 CLEAR_CHAT_BUTTON_SELECTOR , CLEAR_CHAT_CONFIRM_BUTTON_SELECTOR , OVERLAY_SELECTOR ,
1414 PROMPT_TEXTAREA_SELECTOR , RESPONSE_CONTAINER_SELECTOR , RESPONSE_TEXT_SELECTOR ,
15- EDIT_MESSAGE_BUTTON_SELECTOR
15+ EDIT_MESSAGE_BUTTON_SELECTOR , USE_URL_CONTEXT_SELECTOR , UPLOAD_BUTTON_SELECTOR
1616)
1717from config import (
1818 CLICK_TIMEOUT_MS , WAIT_FOR_ELEMENT_TIMEOUT_MS , CLEAR_CHAT_VERIFY_TIMEOUT_MS ,
@@ -59,6 +59,30 @@ async def adjust_parameters(self, request_params: Dict[str, Any], page_params_ca
5959 await self ._adjust_top_p (top_p_to_set , check_client_disconnected )
6060 await self ._check_disconnect (check_client_disconnected , "End Parameter Adjustment" )
6161
62+ # 调整URL CONTEXT
63+ await self ._open_url_content (check_client_disconnected )
64+
65+ async def _open_url_content (self ,check_client_disconnected : Callable ):
66+ try :
67+ collapse_tools_locator = self .page .locator ('button[aria-label="Expand or collapse tools"]' )
68+ grandparent_locator = collapse_tools_locator .locator ("xpath=../.." )
69+
70+ # 3. 获取祖父级元素的 class 属性值
71+ # get_attribute 返回一个包含所有 class 的字符串,例如 "menu dropdown active"
72+ class_string = await grandparent_locator .get_attribute ("class" )
73+
74+ # 4. 在 Python 中进行判断
75+ # 确保 class_string 不是 None,并且 'expanded' 是一个独立的 class
76+ if class_string and "expanded" not in class_string .split ():
77+ await collapse_tools_locator .click (timeout = CLICK_TIMEOUT_MS )
78+ await asyncio .sleep (0.5 )
79+ use_url_content_selector = self .page .locator (USE_URL_CONTEXT_SELECTOR )
80+ is_checked = await use_url_content_selector .get_attribute ("aria-checked" )
81+ if "false" == is_checked :
82+ await use_url_content_selector .click (timeout = CLICK_TIMEOUT_MS )
83+ await self ._check_disconnect (check_client_disconnected , "点击URLCONTEXT" )
84+ except Exception as e :
85+ self .logger .error (f"[{ self .req_id } ] ❌ 操作USE_URL_CONTEXT_SELECTOR时发生错误:{ e } 。" )
6286
6387 async def _adjust_temperature (self , temperature : float , page_params_cache : dict , params_cache_lock : asyncio .Lock , check_client_disconnected : Callable ):
6488 """调整温度参数。"""
@@ -76,6 +100,7 @@ async def _adjust_temperature(self, temperature: float, page_params_cache: dict,
76100 self .logger .info (f"[{ self .req_id } ] 请求温度 ({ clamped_temp } ) 与缓存值 ({ cached_temp } ) 不一致或缓存中无值。需要与页面交互。" )
77101 temp_input_locator = self .page .locator (TEMPERATURE_INPUT_SELECTOR )
78102
103+
79104 try :
80105 await expect_async (temp_input_locator ).to_be_visible (timeout = 5000 )
81106 await self ._check_disconnect (check_client_disconnected , "温度调整 - 输入框可见后" )
@@ -421,7 +446,7 @@ async def _verify_chat_cleared(self, check_client_disconnected: Callable):
421446 except Exception as verify_err :
422447 self .logger .warning (f"[{ self .req_id } ] ⚠️ 警告: 清空聊天验证失败 (最后响应容器未隐藏): { verify_err } " )
423448
424- async def submit_prompt (self , prompt : str , check_client_disconnected : Callable ):
449+ async def submit_prompt (self , prompt : str ,image_list : List , check_client_disconnected : Callable ):
425450 """提交提示到页面。"""
426451 self .logger .info (f"[{ self .req_id } ] 填充并提交提示 ({ len (prompt )} chars)..." )
427452 prompt_textarea_locator = self .page .locator (PROMPT_TEXTAREA_SELECTOR )
@@ -446,8 +471,34 @@ async def submit_prompt(self, prompt: str, check_client_disconnected: Callable):
446471 await autosize_wrapper_locator .evaluate ('(element, text) => { element.setAttribute("data-value", text); }' , prompt )
447472 await self ._check_disconnect (check_client_disconnected , "After Input Fill" )
448473
474+ # 上传
475+ if len (image_list ) > 0 :
476+ try :
477+ # 1. 监听文件选择器
478+ # page.expect_file_chooser() 会返回一个上下文管理器
479+ # 当文件选择器出现时,它会得到 FileChooser 对象
480+ function_btn_localtor = self .page .locator ('button[aria-label="Insert assets such as images, videos, files, or audio"]' )
481+ await function_btn_localtor .click ()
482+ asyncio .sleep (0.5 )
483+ async with self .page .expect_file_chooser () as fc_info :
484+ # 2. 点击那个会触发文件选择的普通按钮
485+ upload_btn_localtor = self .page .locator (UPLOAD_BUTTON_SELECTOR )
486+ await upload_btn_localtor .click ()
487+ print ("点击了 JS 上传按钮,等待文件选择器..." )
488+
489+ # 3. 获取文件选择器对象
490+ file_chooser = await fc_info .value
491+ print ("文件选择器已出现。" )
492+
493+ # 4. 设置要上传的文件
494+ await file_chooser .set_files (image_list )
495+ print (f"已将 '{ image_list } ' 设置到文件选择器。" )
496+
497+ except Exception as e :
498+ print (f"在上传文件时发生错误: { e } " )
499+
449500 # 等待发送按钮启用
450- wait_timeout_ms_submit_enabled = 40000
501+ wait_timeout_ms_submit_enabled = 100000
451502 try :
452503 await self ._check_disconnect (check_client_disconnected , "填充提示后等待发送按钮启用 - 前置检查" )
453504 await expect_async (submit_button_locator ).to_be_enabled (timeout = wait_timeout_ms_submit_enabled )
0 commit comments