自动登录电脑版微信给指定用户发送消息的python源码,可以用于定时发送信息!
本帖最后由 pnnhnjh 于 2024-8-10 11:54 编辑自动登录电脑版微信给指定用户发送消息的python代码,需要先安装uiautomation库,同时要按实际情况修改微信程序的安装目录,设定收信用户名和要发送的消息。注意:不改变原来在电脑上登录微信的安全验证,如需要扫码、需要手机确认等。
# -*- coding:utf-8 -*-
import psutil# 要额外安装,判断程序是否运行
from time import sleep
import uiautomation# 要额外安装,操控窗口控件
import os
def ifProcessRunning(process_name='WeChat.exe'):
# 判断某个程序是否在运行
# 原理:获取正在运行程序的pid,通过pid获取程序名,再按程序名进行判断
pl = psutil.pids()
result = "PROCESS_IS_NOT_RUNNING"
for pid in pl:
if (psutil.Process(pid).name() == process_name):
if isinstance(pid, int):
result = "PROCESS_IS_RUNNING"
return result
def wechatlogin():
uiautomation.uiautomation.TIME_OUT_SECOND = 2# global time out
if ifProcessRunning()=="PROCESS_IS_RUNNING":
try:
wechatWindow = uiautomation.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='微信')
wechatWindow.Restore()
wechatWindow.Minimize()
except:
try:
uiautomation.PaneControl(searchDepth=1, ClassName='WeChatLoginWndForPC', Name='微信').ButtonControl(Name='仅传输文件')
except:
pass
os.system('taskkill /F /im "WeChat.exe" >nul')
os.startfile(WeChat_path)
loginwin = uiautomation.PaneControl(searchDepth=1, ClassName='WeChatLoginWndForPC', Name='微信')
try:
loginwin.ButtonControl(Name='进入微信').Click()
except:
loginwin.ButtonControl(Name='登录').Click()
else:
os.startfile(WeChat_path)
loginwin = uiautomation.PaneControl(searchDepth=1, ClassName='WeChatLoginWndForPC', Name='微信')
try:
loginwin.ButtonControl(Name='进入微信').Click()
except:
loginwin.ButtonControl(Name='登录').Click()
uiautomation.uiautomation.TIME_OUT_SECOND = 20# global time out
wechatWindow = uiautomation.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='微信')
sleep(10)
try:
wechatWindow.Minimize()
except:
pass
def wechatmsg(send_to='微信用户名',msg='要发送的消息'):
uiautomation.uiautomation.TIME_OUT_SECOND = 20
wechatWindow = uiautomation.WindowControl(searchDepth=1, className='WeChatMainWndForPC', Name='微信')
try:
wechatWindow.Restore()
except:
pass
wechatWindow.MoveToCenter()
wechatWindow.SetTopmost(True)
# sleep(2)
wechatWindow.EditControl(Name="搜索").Click()
wechatWindow.EditControl(Name="搜索").SendKeys(f'{send_to}{{enter}}', 0.5)
wechatWindow.EditControl(Name=send_to).SendKeys(msg,0.03)
wechatWindow.ButtonControl(Name="发送(S)").Click()
wechatWindow.SetTopmost(False)
wechatWindow.Minimize()
def wechatquit():
if ifProcessRunning()=="PROCESS_IS_RUNNING":
os.system('taskkill /F /im "WeChat.exe" >nul')
if __name__ == '__main__':
WeChat_directory = 'D:\soft\WeChat'
WeChat_path= os.path.join(WeChat_directory, 'WeChat.exe')
wechatlogin()
wechatmsg(send_to='微信收信用户名张三',msg='要发送的消息')
wechatmsg(send_to='李四', msg='要发送的消息')
# wechatquit() 有一个病毒,就是会窃取微信控制权,在后台定向找关键字的群发送病毒文件扩散。发完后删除消息内容。造成用户无法撤回,然后直接退群。
你这个有那么一丝丝的苗头 kangta520 发表于 2024-8-10 13:40
效果图放出来看看
就是微信电脑版的界面,登录--查找用户--发送消息--最小化或退出。注要是供其它程序调用,如定时发送消息、定时接受邮件后发送消息给接收人、其它系统事件触发时发消息给维护人员。 效果图放出来看看 感谢分享,学到了 6,还得是大佬们厉害啊 感谢分享 感谢感谢 厉害,我去跑跑 wechatWindow 能抓住这个句柄,是第一步,用了枚举吗?