您的位置:首页 >RPA实战:用FlaUI自动发送微信消息
发布于2025-08-20 阅读(0)
扫一扫,手机访问
一直以来,我都希望能够实现微信的群发功能,但由于担心违法而未能实现。记得某公司因为破解了微信的接口,结果被起诉并罚了巨额罚款。
这时,我想到,如果我采用RPA技术,就不会对微信造成任何影响,因为我只是在模拟自己的电脑操作,微信公司也无法识别我的行为是否违法。
因此,这种基于安全技术的方法可能就是可行的。
于是,我尝试了一下,同时也参考了网络上的资料,这给了我一些启发。
一、什么是FlaUI?FlaUI 是一个基于微软 UIAutomation 技术的工具,自从Windows Vista发布以来,它提供了一套新的UI自动化测试技术,简称UIA。在最新的Windows SDK中,UIA与MSAA等其他支持UI自动化的组件一起发布,称为Windows Automation API。
UIA定义了一套全新的、专为UI自动化设计的接口和模式。包括用于遍历和条件查询UI元素的TreeWalker/FindAll,定义了UI元素属性的UIA Property,如Name、ID、Type、ClassName、Location、Visibility等。还定义了UI元素行为的UIA Pattern,如Select、Expand、Resize、Check、Value等。此外,还引入了UIA Event接口,允许测试程序在特定事件发生后得到通知,例如新窗口打开事件等。
目前,FlaUI使用的是UIA2和UIA3两种技术。我主要使用的是UIA3。
二、使用步骤
Install-Package FlaUI.UIA3 -Version 3.2.0
Process[] processes = Process.GetProcessesByName("WeChat");
if (processes.Count() != 1)
{
Console.WriteLine("微信未启动或启动多个微信");
}
else
{
//1.附加到微信进程
using (var app = Application.Attach(processes.First().Id))
{
using (var automation = new UIA3Automation())
{
//2.获取主界面
var mainWindow = app.GetMainWindow(automation);
Console.WriteLine("获取主界面");
//3.切换到通讯录
var elements = mainWindow.FindAll(FlaUI.Core.Definitions.TreeScope.Subtree, TrueCondition.Default);
var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("通讯录"));
addressBook.DrawHighlight(System.Drawing.Color.Red);
var path = Debug.GetXPathToElement(addressBook);
Console.WriteLine("点击通讯录");
addressBook.Click();
//4.搜索
string target = "文件传输助手";
var searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("搜索")).AsTextBox();
searchTextBox.Click();
Keyboard.Type(target);
Keyboard.Type(VirtualKeyShort.RETURN);
Console.WriteLine("搜索目标对象");
//5.切换到对话框
Thread.Sleep(500);
var searchList = mainWindow.FindFirstDescendant(cf => cf.ByName("搜索结果"));
if (searchList != null)
{
var searchItem = searchList.FindAllDescendants().FirstOrDefault(cf => cf.Name == target && cf.ControlType == FlaUI.Core.Definitions.ControlType.ListItem);
searchItem?.DrawHighlight(System.Drawing.Color.Red);
searchItem?.AsListBoxItem().Click();
}
else
{
Console.WriteLine("没有搜索到内容");
}
Thread.Sleep(500);
//6.输入文本
string sendMsg = "这个是我微信的输入信息:" + DateTime.Now.ToString();
var msgInput = mainWindow.FindFirstDescendant(cf => cf.ByName("输入")).AsTextBox();
msgInput?.Click();
System.Windows.Forms.Clipboard.SetText(sendMsg);
Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });
var sendBtn = mainWindow.FindFirstDescendant(cf => cf.ByName("sendBtn"));
sendBtn?.DrawHighlight(System.Drawing.Color.Red);
sendBtn?.Click();
}
}
}这段代码带有注释,易于理解。它的功能是搜索特定的人,然后向其发送指定的信息,完成任务。
图示效果如下:

Process[] processes = Process.GetProcessesByName("WeChat");
if (processes.Count() != 1)
{
Console.WriteLine("微信未启动或启动多个微信");
}
else
{
//1.附加到微信进程
using (var app = Application.Attach(processes.First().Id))
{
using (var automation = new UIA3Automation())
{
//2.获取主界面
var mainWindow = app.GetMainWindow(automation);
Console.WriteLine("获取主界面");
//3.切换到聊天目录
var elements = mainWindow.FindAll(TreeScope.Subtree, TrueCondition.Default);
var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("聊天"));
addressBook.DrawHighlight(System.Drawing.Color.Red);
var path = Debug.GetXPathToElement(addressBook);
addressBook.Click();
Console.WriteLine("切换到聊天");
Thread.Sleep(2000);
//4.获取聊天列表
//只发前六个
var count = 0;
var searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("会话")).AsListBoxItem();
while (searchTextBox != null)
{
var list = searchTextBox.FindAllChildren();
foreach (var item in list)
{
count++;
var name = item.Name;
item.Click();
var type = item.ControlType;
item.DrawHighlight(System.Drawing.Color.Red);
var MsgSend = mainWindow.FindFirstDescendant(cf => cf.ByName("输入")).AsTextBox();
var MsgSendButton = mainWindow.FindFirstDescendant(cf => cf.ByName("sendBtn"));
if (MsgSend != null && MsgSendButton != null)
{
MsgSend.Click();
System.Windows.Forms.Clipboard.SetText($"群发消息,请忽略:{DateTime.Now}");
Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });
MsgSendButton.Click();
Console.WriteLine($"发送信息:{name}");
Thread.Sleep(500);
}
if (count == 6)
{
break;
}
}
if (count == 6)
{
break;
}
for (int i = 0; i < 5; i++)
{
Keyboard.Type(VirtualKeyShort.DOWN);
}
searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("会话")).AsListBoxItem();
Thread.Sleep(2000);
}
}
}
}这个代码的主要作用是向前六个会话发送群发消息,如果会话没有发送按钮,则不会发送,以避免影响更多人。
图示效果如下:

我录制了好几次,最后还有人把我删除了,尴尬。
可以通过FlaUinspect工具来获取页面的信息。
FlaUInspect[1]

可以通过以下方式看到XPath地址:


这个FlaUinspect项目是一个WPF项目,如果你想深入研究,可以查看源码,并进行跟踪调试。
主要可以通过以下两种方式获取所需的内容:
第一种是通过同一页面独一无二的名字来获取:
var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("聊天"));第二种是通过XPath地址来找到所需的控件:
var infoData = automationElement.FindAllByXPath("/Pane/Pane[1]");总结
总的来说,这种技术还是非常方便的,但对于使用自绘技术的QQ以及基于QT、JAVA的应用程序可能无法实现,只能针对微软的技术产品,如WinForms和WPF等。
总体来说,它降低了使用时的难度。
例如,这个微信发送信息的功能,你可以自己扩展,比如指定人发送、群发、定时发送、标签发送等,对于个人来说,作用还是不错的。
参考资料[1]
FlaUInspect: https://github.com/FlaUI/FlaUInspect
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
4
5
6
7
8
9