经查询资料,一共有几款软件可以实现,虽然最后只用到了其中一种,但其他软件也值得记录一下,这次没用上,下次说不定就用上了。
- Power Automate Desktop:微软产品,免费,win11自带,win10可在微软商店下载。拖拽式开发,使用时需要先登录,我试了几次没登录成功,所以就放弃了,感觉国外的产品在登录进都很困难。而且我看了下操作界面,比较复杂,应该不容易上手。
- 影刀:国内软件,拖拽式开发,操作界面看起来比Automate要简洁一些,我简单试了几次,还是感觉不好上手,所以又放弃了。
由于我本人一直对拖拽式开发不习惯,所以把精力又转向代码式开发。 - autohotkey:感觉语法很奇怪,理解起来有难度,又大概看了看介绍,好像是做类似按键精灵的功能用的,跟我的需求不相符。
- PyAutoGUI:需要给出聊天框的位置,感觉麻烦,没多用。
- uiautomation:成功实现,它的原理应该是遍历windows窗口中的每一个元素,直到找到你想要的内容,在windows中一行文字也是一个元素。
在使用uiautomation时还要用到一款配合软件:Accessibility Insights,用于查看窗口中每个元素的属性。
下载地址:https://accessibilityinsights.io/downloads/

77a00735f1ef6b8929c109f1e6d62a0f.png
使用方式是点击你要关注的窗口元素,软件里就会显示出该元素的相关属性。
import uiautomation as auto
import time
def main():
auto.SetGlobalSearchTimeout(15) # 全局超时设为15秒
window = auto.WindowControl(Name="***") # 输窗口名称,我这里就是聊天软件名称
if not window.Exists(10):
print("错误:未找到'***'窗口")
return
window.SetActive()
last_full_content = ""
current_content = ""
while True:
try:
# 1. 定位chatbox文档(增加搜索深度)
conv_list = window.ListControl(Name="convlist")
chatbox = window.DocumentControl(Name="chatbox")
for control, depth in auto.WalkControl(chatbox):
if control.ControlType == auto.ControlType.GroupControl and len(control.GetChildren()) > 0 and control.GetFirstChildControl().ControlType == auto.ControlType.TextControl:
message = []
for item in control.GetChildren():
# print(f"chatbox子控件类型: {item.ControlTypeName}, 名称: {item.Name}")
message.append(item.Name)
current_content = "\n".join(message)
print(current_content)
print(last_full_content)
if current_content != last_full_content:
last_full_content = current_content
input_edit = window.EditControl(Name="请输入消息")
input_edit.Click()
auto.SendKeys("你好")
auto.SendKeys("{ENTER}")
except Exception as e:
print(f"定位异常:{str(e)}")
if __name__ == '__main__':
main()
还有一款不错的开源项目估计也能实现,以后空了再研究
github地址:https://github.com/openinterpreter/open-interpreter