当前位置:   article > 正文

FlaUI操作微信发消息,报FlaUI.Core.Exceptions.NoClickablePointException异常_flaui.core 微信 公众号

flaui.core 微信 公众号
  1. using FlaUI.Core.AutomationElements;
  2. using FlaUI.Core.Input;
  3. using FlaUI.Core.WindowsAPI;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Diagnostics;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. namespace WindowsFormsApp1
  16. {
  17. public partial class Form1 : Form
  18. {
  19. public Form1()
  20. {
  21. InitializeComponent();
  22. }
  23. private void button1_Click(object sender, EventArgs e)
  24. {
  25. Process[] processes = Process.GetProcessesByName("WeChat");
  26. if (processes.Count() != 1)
  27. {
  28. Console.WriteLine("微信未启动或启动多个微信");
  29. }
  30. else
  31. {
  32. //1.附加到微信进程
  33. using (var app = FlaUI.Core.Application.Attach(processes.First().Id))
  34. {
  35. using (var automation = new FlaUI.UIA3.UIA3Automation())
  36. {
  37. //2.获取主界面
  38. var mainWindow = app.GetMainWindow(automation);
  39. Console.WriteLine("获取主界面");
  40. //3.切换到通讯录
  41. var elements = mainWindow.FindAll(FlaUI.Core.Definitions.TreeScope.Subtree, FlaUI.Core.Conditions.TrueCondition.Default);
  42. var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("通讯录"));
  43. addressBook.DrawHighlight(System.Drawing.Color.Red);
  44. var path = FlaUI.Core.Debug.GetXPathToElement(addressBook);
  45. Console.WriteLine("点击通讯录");
  46. addressBook.Click();
  47. //4.搜索
  48. string target = "文件传输助手";
  49. var searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("搜索")).AsTextBox();
  50. searchTextBox.Click();
  51. Keyboard.Type(target);
  52. Keyboard.Type(VirtualKeyShort.RETURN);
  53. Console.WriteLine("搜索目标对象");
  54. //5.切换到对话框
  55. Thread.Sleep(1000);
  56. var searchList = mainWindow.FindFirstDescendant(cf => cf.ByName("搜索结果"));
  57. if (searchList != null)
  58. {
  59. var searchItem = searchList.FindAllDescendants().FirstOrDefault(cf => cf.Name == target && cf.ControlType == FlaUI.Core.Definitions.ControlType.ListItem);
  60. searchItem?.DrawHighlight(System.Drawing.Color.Red);
  61. searchItem?.AsListBoxItem().Click();
  62. }
  63. else
  64. {
  65. Console.WriteLine("没有搜索到内容");
  66. }
  67. Thread.Sleep(1000);
  68. //6.输入文本
  69. string sendMsg = "这个是我微信的输入信息:" + DateTime.Now.ToString();
  70. var msgInput = mainWindow.FindFirstDescendant(cf => cf.ByName("输入")).AsTextBox();
  71. msgInput?.Click();
  72. System.Windows.Forms.Clipboard.SetText(sendMsg);
  73. Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });
  74. var sendBtn = mainWindow.FindFirstDescendant(cf => cf.ByName("sendBtn"));
  75. sendBtn?.DrawHighlight(System.Drawing.Color.Red);
  76. sendBtn?.Click();
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号