目录
导入需要用的包
打开微信,设置好相关信息
获取微信的PID端口号,并获取主窗口
搜索联系人
发送消息
导入需要用的包
import psutil
import pyautogui
from pywinauto.application import Application
打开微信,设置好相关信息
# 打开微信的快捷键
pyautogui.hotkey('ctrl', 'alt', 'w')
# 需要发送者消息的人
searchUserName = '文件传输助手'
# 消息内容
sendMsg = "That this hates are continuous actually and longer than the sky and earth."
获取微信的PID端口号,并获取主窗口
PID = 0
for proc in psutil.process_iter():try:pinfo = proc.as_dict(attrs=['pid', 'name'])except psutil.NoSuchProcess:passelse:if 'WeChat.exe' == pinfo['name']:PID = pinfo['pid']app = Application(backend='uia').connect(process=PID)
main_Win = app.window(class_name='WeChatMainWndForPC')
搜索联系人
selectItem = main_Win.child_window(title=searchKey, control_type="ListItem").wrapper_object()
selectItem.draw_outline(colour='red')
selectItem.click_input()
发送消息
inputMsg = main_Win.child_window(title="输入", control_type="Edit").wrapper_object()
inputMsg.click_input()
inputMsg.type_keys(sendMsg, with_spaces=True)# 发送
# sendbtn = main_Win["sendBtn"]
# sendbtn.draw_outline(colour='red')
# sendbtn.click_input()# 回车发送
pyautogui.hotkey('enter')