FlaUI 操作微信发送消息 FlaUI 操作微信发送消息-文本
微信操作发消息。
不要用NUGET安装FlaUI,不然会各种报错。 在Github 下载源码,用visual studio 2019 打开,等待解决方案加载完毕。
解决方案上点击右键,生成解决方案,等待项目编译完成。
查看 目录 FlaUI.UIA3\bin\Debug
编译出来对应的.NET 版本.
这里 我们在FlaUI解决方案里面新建一个项目,新建完成后,添加 FlaUI 引用。我选的.NET版本是4.5 就引用 FlaUI.Core.dll,FlaUI.UIA3.dll 两个文件。
微信群发
Process[] processes = Process.GetProcessesByName("WeChat"); if (processes.Count() != 1) { Console.WriteLine("微信未启动或启动多个微信"); } else { //1.附加到微信进程 using (var app = FlaUI.Core.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 = FlaUI.Core.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 < list.Length; i++) { searchTextBox.Focus(); Keyboard.Press(VirtualKeyShort.DOWN); Thread.Sleep(100); } searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("会话")).AsListBoxItem(); Thread.Sleep(2000); } } } }
微信单独发送测试(发送到文件助手)
Process[] processes = Process.GetProcessesByName("WeChat"); if (processes.Count() != 1) { Console.WriteLine("微信未启动或启动多个微信"); } else { //1.附加到微信进程 using (var app = FlaUI.Core.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 = FlaUI.Core.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(); } } }