This commit is contained in:
HuangHai
2026-01-31 07:54:15 +08:00
parent 54f1b2fb91
commit cb6ddbb76d
3 changed files with 35 additions and 8 deletions

View File

@@ -74,18 +74,33 @@ def main():
# - Input text
# - Find and click Send button
logger.info("Step 1 & 2: Finding input box and writing text...")
logger.info("Step 1: Finding input box and writing text...")
# We can get a rough position first for logging, though perform_input_action is more robust
screenshot_path = os.path.join(WxUtil.OUTPUT_DIR, "T5_Initial_Check.jpg")
d.screenshot(screenshot_path)
input_pos, _ = WxUtil.find_input_box_center(screenshot_path)
logger.info(f"Initial target input position: {input_pos}")
# We pass debug_prefix="T5" to perform_input_action to get intermediate screenshots
# The function will save:
# - T5_1_check_mode.jpg
# - T5_2_after_mode.jpg
# - T5_3_input_box_found.jpg (if template matched)
# - T5_4_after_input.jpg
# - T5_5_after_send.jpg
# Initial state screenshot
initial_path = os.path.join(WxUtil.OUTPUT_DIR, "T5_0_Initial.jpg")
d.screenshot(initial_path)
logger.info(f"Initial state screenshot saved: {initial_path}")
success = WxUtil.perform_input_action(d, input_pos, test_text, auto_send=True)
# Use a more robust way to find input_pos for logging
input_pos, _ = WxUtil.find_input_box_center(initial_path)
logger.info(f"Target input position (estimated): {input_pos}")
success = WxUtil.perform_input_action(d, input_pos, test_text, auto_send=True, debug_prefix="T5")
if success:
logger.info("✅ Step 3: Send action completed successfully.")
# Final state screenshot
final_path = os.path.join(WxUtil.OUTPUT_DIR, "T5_6_Final.jpg")
d.screenshot(final_path)
logger.info(f"Final state screenshot saved: {final_path}")
else:
logger.error("❌ Send action failed.")

View File

@@ -1186,13 +1186,21 @@ def find_all_template_matches(screen_path, template_path, threshold=0.8):
logger.error(f"find_all_template_matches failed: {e}")
return []
def perform_input_action(d, center_point, text, auto_send=True):
def perform_input_action(d, center_point, text, auto_send=True, debug_prefix=None):
"""
执行输入操作
:param debug_prefix: 如果提供,将在关键步骤保存截图,如 {debug_prefix}_before_mode.jpg
"""
try:
def save_debug_shot(name):
if debug_prefix:
shot_path = os.path.join(OUTPUT_DIR, f"{debug_prefix}_{name}.jpg")
d.screenshot(shot_path)
logger.info(f"保存中间过程截图: {shot_path}")
# --- 新增逻辑:确保处于文字输入模式 ---
logger.info("正在检查输入模式...")
save_debug_shot("1_check_mode")
# 优先使用 uiautomator2 的属性检测(比图像识别更稳)
# 1. 检查是否有 "切换到键盘" 按钮(说明当前是语音模式)
@@ -1230,6 +1238,7 @@ def perform_input_action(d, center_point, text, auto_send=True):
except:
pass
# --- 新增逻辑结束 ---
save_debug_shot("2_after_mode")
# 1. 尝试找到原生输入框并输入
# 增加多种查找方式
@@ -1252,6 +1261,7 @@ def perform_input_action(d, center_point, text, auto_send=True):
if input_pos:
logger.info(f"✅ [Template] 通过 input_text.jpg 找到输入框: {input_pos}")
save_debug_shot("3_input_box_found")
# 绘制调试图 (蓝框)
try:
@@ -1335,6 +1345,7 @@ def perform_input_action(d, center_point, text, auto_send=True):
# 尝试粘贴
d.press("paste")
save_debug_shot("4_after_input")
time.sleep(1.0)
# 3. 发送
@@ -1360,6 +1371,7 @@ def perform_input_action(d, center_point, text, auto_send=True):
d.press("enter")
logger.info("Pressed Enter")
save_debug_shot("5_after_send")
# 清理临时文件
if os.path.exists(tmp_screen):
try: