获取微信进程 PID
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['pid', 'name'])
except psutil.NoSuchProcess:
pass
else:
if 'WeChat.exe' == pinfo['name']:
PID = pinfo['pid']
# -*- coding:utf-8 -*-
import psutil
import pywinauto
from pywinauto.application import Application
import time
def getWinxin(number):
# 获取微信的 PID
PID = 0
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['pid', 'name'])
except psutil.NoSuchProcess:
pass
else:
if 'WeChat.exe' == pinfo['name']:
PID = pinfo['pid']
# 连接 微信
app = Application(backend='uia').connect(process=PID)
win = app[u'微信']
# 查到微信的控件树
# print(win.print_control_identifiers())
# 点击通讯录
# addresslist = win.child_window(title=u"通讯录", control_type="Button")
# addresslist.draw_outline()
# cords = addresslist.rectangle()
# pywinauto.mouse.click(button='left', coords=(cords.left + 10, cords.top + 10))
# win.draw_outline()
# 点击搜索
addresslist = win.child_window(title="搜索", control_type="Edit")
cords = addresslist.rectangle()
pywinauto.mouse.click(button='left', coords=(cords.left, cords.top))
pywinauto.mouse.click(button='left', coords=(cords.left, cords.top))
win.Edit.type_keys((number), with_spaces=True)
time.sleep(1)
# 输入姓名并回车
win.type_keys('{ENTER}')
win.Edit.type_keys(("微信机器人自动回复,,"), with_spaces=True).type_keys('{ENTER}')
if __name__ == '__main__':
# 测试,
# 输入微信号, 或者备注姓名
# 微信号搜的更准确
# getWinxin("文件传输助手")
for i in ['文件传输助手', "微信团队"]:
getWinxin(i)
问题: send_key 无法导入的话, 从 GitHub 下载 whl 本地安装
转载: https://www.jb51.net/article/181580.htm
参考: https://pywinauto.readthedocs.io/en/latest/