1  首先安装uiautomation

   pip install uiautomation

2 给微信“文件传输助手”发送消息

import time
import uiautomation as auto


def findNickname(wechat_id,msg = ""):
    # 打开微信
    wechat_window = auto.WindowControl(searchDepth=1, ClassName='WeChatMainWndForPC', SubName='微信')  # 找到微信窗口的句柄
    if not wechat_window.Exists(5):
        print("微信窗口未找到或超时。")
        return

    wechat_window.SetActive()  # 将微信窗口显示出来
    time.sleep(1)

   # 点击通讯录
   # wechat_window.ButtonControl(Name="通讯录").Click()

    # 定位搜索框并输入微信号
    search_box = wechat_window.EditControl(Name="搜索")  # 获得搜索框的句柄
    if search_box.Exists():
        search_box.Click()              # 点击搜索框
        search_box.SendKeys(wechat_id)  # 输入搜索内容
        # 模拟回车键来执行搜索操作
        auto.SendKeys('{ENTER}')        #  回车确认
        time.sleep(1)

        contacts_window = auto.GetForegroundControl()  # 获取当前被激活的窗口
        #print(contacts_window)
        if contacts_window.Name == '微信':              # 判断当前被激活的窗口是微信
            contacts_window.SendKeys(msg)               #发送消息
            sendButton = contacts_window.ButtonControl(Name='发送(S)') # 获取发送按钮的句柄
            sendButton.Click()                           # 点击发送
            print("消息发送完成")
        
    else:
        print("搜索框未找到或超时。")
        return


if __name__ == "__main__":
    target_nick_name = ['文件传输助手']
    for name in target_nick_name:
        findNickname(name, msg= "测试信息发送")

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐