using FlaUI.Core.AutomationElements;
using FlaUI.Core.Input;
using FlaUI.Core.WindowsAPI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
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 FlaUI.UIA3.UIA3Automation())
{
//2.获取主界面
var mainWindow = app.GetMainWindow(automation);
Console.WriteLine("获取主界面");
//3.切换到通讯录
var elements = mainWindow.FindAll(FlaUI.Core.Definitions.TreeScope.Subtree, FlaUI.Core.Conditions.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(1000);
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(1000);
//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();
}
}
}
}
}
}
-
如果抛FlaUI.Core.Exceptions.NoClickablePointException异常,可以下载源码GitHub - FlaUI/FlaUI: UI automation library for .Net 直接引用源码项目,一般都可以避免此异常,如果直接引用nuget包,很多都是报此异常的