整理 | 小耕家的喵大仙
出品 | CSDN(ID:lichao19897314)
Q Q | 978124155
往期知识回顾:
👆 以上文章是以往对使用自动化技术操作微信的一些文章及案例!!!!
上一篇文章介绍了微信UI自动化-微信窗口跟随主窗体移动(C#)
最近也一直没有更新过这块,近期很多朋友咨询我能不能用自动化测试工具获取微信的聊天记录,所以抽空实现了该功能。
功能如下:自动获取左边勾选的联系人,采集指定联系人某日期区间的微信聊天记录。
直接上效果图和视频
获取消息记录
(1)首先搜索根据勾选的搜索联系人
该功能已经在实现 链接如下(6)微信UI自动化-搜索指定联系人(C#) 这里不重复
(2)获取聊天面板中的消息
演示代码获取指定联系人10月10到今天的聊天记录
private void GetMessage(string name)
{
tempCount = 0;
List<WXChatMessage> contents = new List<WXChatMessage>();
string chatPath = "/Pane/Pane[2]/Pane/Pane/Pane/Pane/Pane[2]/Pane[1]/Pane/List";
var chatList = UI_WX_Window.Current.Find(chatPath);
while (true)
{
var chatListItemSource = chatList.FindAllChildren().ToList();
var tempMessageSource = new List<WXChatMessage>();
chatListItemSource.GetRange(0, chatListItemSource.Count - tempCount).ForEach(item =>
{
if (item.ControlType == FlaUI.Core.Definitions.ControlType.ListItem)
{
var my = item.FindFirstByXPath("/Pane/Pane[2]/Pane/Pane/Pane/Text");
var to = item.FindFirstByXPath("/Pane/Pane[1]/Pane/Pane/Pane/Text");
if (my != null)
{
var button = item.FindFirstByXPath("/Pane/Button");
tempMessageSource.Add(new WXChatMessage { Content = my.Name.ToString(), UserName = button.Name.ToString(), Type = 1 });
}
else if (to != null)
{
var button = item.FindFirstByXPath("/Pane/Button");
tempMessageSource.Add(new WXChatMessage { Content = to.Name.ToString(), UserName = button.Name.ToString(), Type = 2 });
}
else
{
var time = item.FindFirstByXPath("/Text");//日期
if (time != null)
{
var s = DateTime.Now;
DateTime.TryParse(item.Name, out s);
tempMessageSource.Add(new WXChatMessage { Time = s == DateTime.MinValue ? DateTime.Now : s, Content = time.Name.ToString(), Type = 3 });
}
}
}
});
contents.InsertRange(0, tempMessageSource);
tempCount = chatListItemSource.Count();
var beTime = DateTime.Parse("2023-10-10");
if (tempMessageSource.Where(b => b.Type == 3 && b.Time <= beTime).Count() > 0)
{
break;
}
//点击查看更多
if (chatListItemSource[0].ControlType == FlaUI.Core.Definitions.ControlType.Button && chatListItemSource[0].Name.ToString() == "查看更多消息")
{
//点击
ScrollTop(chatList, chatListItemSource[0]);
}
else
{
break;
}
}
ChatSource.Add(new WXChat { Name = name, Chats = contents });
}
(3)如果发现未到指定日期需要滚动聊天面板循环获取
private void ScrollTop(FlaUI.Core.AutomationElements.AutomationElement chatList, FlaUI.Core.AutomationElements.AutomationElement more)
{
//该出代码比较长,需要请QQ沟通获取详细源码
}
如果读者对上述文章存在不清晰的地方可以找本人获取运行源码来学习,QQ 978124155