整理 | 小耕家的喵大仙
出品 | CSDN(ID:lichao19897314)
Q Q | 978124155
往期知识回顾:
👆 以上文章是以往对使用自动化技术操作微信的一些文章及案例!!!!
因为文章可能无法满足读者要求,如需源码和支持请联系本人 QQ 978124155
本篇目的
有一位做外卖门店运营的朋友说他有10几家门店,每天需要在固定时间段提醒每个店的店长库存信息,库存信息存储在他自建的ERP系统中,ERP数据能自由获取,就是需要每天人工在每个点发送,有时候一忙或者不在电脑旁就忘记或者无法执行该项工作,所以针对该场景,本人决定基于微信自动化技术帮助他解放这项繁琐的工作。
本篇实现的功能,通过查找微信搜素框搜索到对应的联系人,定位到联系人的聊天界面,将需要发送的内容输入到聊天界面的发送文本框,点击发送按钮,发送信息到对应联系人。
软件效果演示
自动发送消息
实现思路
(1)寻找到微信搜索框,将需要接受消息的联系人输入到搜索框中
(2)如果发现有对应的最优搜索结果,则选择第一个项进行点击操作
(3)定位到需要接受消息的联系人聊天界面,将需要发送的信息填入到聊天界面的输入框中
(4)执行点击发送按钮,执行消息发送。
技术细节
定位消息编辑框
if (SendInput == null)
{
SendInput = UI_WX_Window.Current.Find("/Pane/Pane[2]/Pane/Pane/Pane/Pane[1]/Pane[2]/Pane[2]/Pane/Pane[1]/Edit");
}
向搜索框输入联系人内容
private void SetText(WXMessageEntity msg, AutomationElement ele, bool isClear)
{
UI_WX_Window.Current.ClickElement(ele);
if (isClear)
{
UI_WX_Window.Current.ClearInput();
}
WXUIAuto.Currnet.Invoke(new Action(() =>
{
msg.Copy();
}));
UI_WX_Window.Current.Paste();
}
寻找最优联系人
public bool SearchContract(string name)
{
ClickContacts();
Thread.Sleep(200);
GetSearchElement();
if (Search != null)
{
SystemLog.Info("开始查找联系人【" + name + "】");
SetText(new WXMessageEntity { Info = name, Type = WXMessageType.Text }, Search);
var element = SearchContact(name);
if (element != null)
{
SystemLog.Info("查找联系人【" + name + "】成功,开始发送消息");
UI_WX_Window.Current.ClickElement(element);
return true;
}
else
{
SystemLog.Info("查找联系人【" + name + "】失败");
return false;
}
}
return false;
}
}
protected AutomationElement SearchContact(string name)
{
for (int i = 0; i < 15; i++)
{
var currentConract = UI_WX_Window.Current.Find("/Pane[1]/Pane[1]/Pane[2]/List/ListItem[1]");
if (currentConract != null
&&
currentConract.Name == name)
{
return currentConract;
}
Thread.Sleep(100);
}
return null;
}
获取发送消息按钮的元素信息
if (SendButton == null)
{
SendButton = UI_WX_Window.Current.Find("/Pane/Pane[2]/Pane/Pane/Pane/Pane/Pane[2]/Pane[2]/Pane[2]/Pane/Pane[2]/Pane[3]/Button");
}
发送微信消息给指定联系人
/// <summary>
/// 发送微信消息
/// </summary>
/// <param name="msg"></param>
public void SendWXMsg(string name)
{
if (SendInput == null)
{
SendInput = GetSendInput();
}
if (SendInput != null)
{
WXSendMessageSource.Current.ForEach(msg =>
{
SetText(msg, SendInput, false);
});
if (SendButton == null)
{
SendButton = GetSendButton();
}
if (SendButton != null)
{
UI_WX_Window.Current.ClickElement(SendButton);
SystemLog.Info("向联系人【" + name + "】发送消息成功");
}
}
else
{
SystemLog.Error("【" + name + "】不是有效联系人");
}
}
if (SendButton != null)
{
UI_WX_Window.Current.ClickElement(SendButton);
SystemLog.Info("向联系人【" + name + "】发送消息成功");
}
因为文章可能无法满足读者要求,如需源码和支持请联系本人 QQ 978124155 上一篇(6)微信UI自动化-搜索指定联系人(C#)https://blog.csdn.net/lichao19897314/article/details/122962242
下一篇(8)微信UI自动化-监控微信进程运行状态(C#)https://blog.csdn.net/lichao19897314/article/details/129762864