求助:使用pywinauto获取filetest.exe面板文本返回空值
解决pywinauto无法获取编辑面板文本的问题
问题分析
你用window_text()方法获取黄色框标注的DataEditor面板文本时返回空,核心原因是:这类多行/富文本编辑控件的内容并不存储在窗口文本(window_text)中,window_text()通常只返回控件的标题或简短标识,而非实际编辑内容。
解决方案
1. 替换获取方法为get_value()
对于标准Windows编辑控件(包括多行编辑框),pywinauto提供了get_value()方法专门获取编辑内容,替换原代码中的window_text()即可:
from pywinauto import Application from pywinauto.timings import wait_until app = Application().start(r"C:\Users\test\Documents\FileTest.exe") dlg = app.window(title_re=".*FileTest.*") tab_control = dlg['TabControl'] tab_control.select('ReadWrite') dlg['ReadFile Button'].click() # 定位DataEditor控件 get_object = dlg[':DataEditor'] # 等待控件加载完成(避免点击后内容还未渲染) wait_until(10, 0.5, lambda: len(get_object.get_value()) > 0) # 用get_value()获取内容 get_value = get_object.get_value() print(f"get_value: {get_value}") print("Automation complete and operation completed successfully!")
2. 精准定位控件(如果上述方法仍失效)
如果控件是富文本控件(比如类名为RichEdit20W),可以通过类名+Automation ID精准定位,再尝试获取内容:
# 替换原定位代码 get_object = dlg.child_window(auto_id="DataEditor", class_name="RichEdit20W") # 对于RichEdit控件,也可尝试texts()方法 get_value = get_object.texts()[0] if get_object.texts() else ""
3. 检查控件状态
- 确保点击
ReadFile后,文本确实已经加载到面板中(可手动操作验证) - 若控件加载较慢,延长等待时间(调整
wait_until的第一个参数,比如改为15)
内容的提问来源于stack exchange,提问作者sulabh poddar
相关产品推荐
相关产品推荐

