当前位置:   article > 正文

UIAutomation实战:自动回复

UIAutomation实战:自动回复

1、导入可能需要用到的第三方库

  1. import time
  2. from datetime import datetime
  3. import uiautomation as auto
  4. import pyperclip
  5. import pyautogui
  6. import pygetwindow
  7. import sys
  8. import subprocess
  9. import SparkApi
  10. import AskSpark

2、初始化一些想要自动化控制的软件或者程序

  1. def initialization_qn():
  2.     # subprocess.Popen(['千牛工作台.exe'], shell=False)
  3.     # 避免使用shell=True:在某些情况下,将 shell=True 设置为 subprocess.Popen 可能导致字符编码问题。尝试删除 shell=True,并将命令和参数分开传递
  4.     floating_bar = auto.WindowControl(searchDepth=1, ClassName='Qt5152QWindowToolSaveBits')
  5.     if floating_bar:
  6.         print("千牛已经完成初始化")
  7.     else:
  8.         print("打开千牛工作台")
  9.         app_path = r"D:\Program Files\AliWorkbench\AliWorkbench.exe"
  10.         args = [app_path, "/run:desktop"]
  11.         subprocess.Popen(args, shell=False)
  12.         time.sleep(30)
  13.     print("打开千牛工作台接待中心")
  14.     floating_bar = auto.WindowControl(searchDepth=1, ClassName='Qt5152QWindowToolSaveBits')
  15.     if not floating_bar:
  16.         print("千牛工作台未完成初始化,程序有问题")
  17.         sys.exit()
  18.     print(floating_bar.Name)
  19.     floating_bar.Click(x=110, y=55, simulateMove=True)
  20.     print("切换到接待中心联系中名单")
  21.     rc_window = auto.WindowControl(searchDepth=1, ClassName='Qt5152QWindowIcon')
  22.     rc_window.Click(x=106, y=170, simulateMove=True)
  23.     # 150 290
  24.     print("切换到第一个联系人")
  25.     rc_window.Click(x=150, y=290, simulateMove=True)
  26.     # 535 500
  27.     print("移动鼠标聚焦聊天信息框")
  28.     rc_window.MoveCursorToInnerPos(x=535, y=500, simulateMove=True)

3、自定义一些可能用得到的UIautomation库的一些常用自定义方法

获取所有同级控件

  1. def get_all_siblings(start_control):
  2.     # 创建一个列表来存储同级控件
  3.     siblings = []
  4.     # 首先获取第一个同级控件
  5.     sibling = start_control.GetNextSiblingControl()
  6.     while sibling is not None and sibling != auto.GetRootControl() and sibling:
  7.         # 添加找到的同级控件到列表中
  8.         siblings.append(sibling)
  9.         # 获取下一个同级控件,直到没有更多
  10.         sibling = sibling.GetNextSiblingControl()
  11.     # 返回包含所有同级控件的列表
  12.     return siblings

获取光标下的控件详细信息
  1. def get_control_from_cursor():
  2.     time.sleep(3)
  3.     idea_control = auto.ControlFromCursor()
  4.     if idea_control is not None and idea_control != auto.GetRootControl():
  5.         print("**********")
  6.         print("能够识别光标下的UI控件")
  7.         auto.EnumAndLogControlAncestors(control=idea_control, showAllName=True, showPid=True)
  8.         if idea_control.Name != "":
  9.             print("**********")
  10.             print(f"该控件有名字:{idea_control.Name}")
  11.         all_children = []
  12.         all_children = idea_control.GetChildren()
  13.         if all_children:
  14.             print("**********")
  15.             print("该控件有子控件:")
  16.             for item in all_children:
  17.                 if item:
  18.                     print(auto.EnumAndLogControlAncestors(control=item, showAllName=True, showPid=True))
  19.         else:
  20.             print("**********")
  21.             print("该控件没有子控件")
  22.         all_siblings = []
  23.         all_siblings = get_all_siblings(idea_control)
  24.         if all_siblings:
  25.             print("**********")
  26.             print("该控件有同级控件:")
  27.             for item in all_siblings:
  28.                 print(auto.EnumAndLogControlAncestors(control=item, showAllName=True, showPid=True))
  29.         else:
  30.             print("**********")
  31.             print("该控件没有同级控件")
  32.         return idea_control
  33.     else:
  34.         print("**********")
  35.         print("无法识别光标下的UI控件")
  36.         return None

获取光标下的控件精简信息

  1. def get_control_from_cursor2():
  2.     time.sleep(3)
  3.     idea_control = auto.ControlFromCursor()
  4.     if idea_control:
  5.         print(idea_control)
  6.         print(idea_control.Name)
  7.         rect = (idea_control.BoundingRectangle.left, idea_control.BoundingRectangle.top, idea_control.BoundingRectangle.right, idea_control.BoundingRectangle.bottom)
  8.         rect_wh = (idea_control.BoundingRectangle.width(), idea_control.BoundingRectangle.height())
  9.         print(rect)
  10.         print(rect_wh)
  11.         return idea_control, rect, rect_wh

获取区域内的有效文本

  1. def get_text_area(start_x, start_y, end_x, end_y):
  2.     auto.WheelUp(1)
  3.     auto.WheelDown(3)
  4.     auto.MoveTo(start_x, start_y)
  5.     auto.PressMouse(start_x, start_y)
  6.     auto.MoveTo(end_x, end_y)
  7.     auto.ReleaseMouse()
  8.     auto.SendKeys('{Ctrl}c')
  9.     # auto.Click()
  10.     text = ""
  11.     text = pyperclip.paste()
  12.     message = text.split('\n')
  13.     # 使用filter函数过滤掉空字符串
  14.     message = list(filter(lambda x: x.strip() != '', message))
  15.     # # 使用列表推导过滤掉空字符串
  16.     # message = [line for line in message if line.strip() != '']
  17.     # 从最后一个子项开始向前遍历
  18.     m_message = ""
  19.     for i in range(-1, -len(message) - 1, -1):
  20.         if message[i] != "已读" and message[i] != "未读" and message[i]:
  21.             m_message = message[i]
  22.             break
  23.     if m_message:
  24.         print("**********")
  25.         print(m_message)
  26.         return m_message

示例

  1. if __name__ == '__main__':
  2.     # idea_c = get_control_from_cursor()
  3.     initialization_qn()
  4.     ic, rect, rect_wh = get_control_from_cursor2()
  5.     ms = get_text_area(rect[0], rect[1], rect[2], rect[3])
  6.     print(f"发送问题:{ms}")
  7.     q = AskSpark.ask(ms)
  8.     print(f"AI回答:{q}")
  9.     print("激活发送消息栏")
  10.     rc_window = auto.WindowControl(searchDepth=1, ClassName='Qt5152QWindowIcon')
  11.     # 1200 950
  12.     rc_window.Click(x=530, y=650, simulateMove=True)
  13.     auto.SendKeys(q)
  14.     auto.SendKey(0x0D)
  15.     # auto.SendKey('{Enter}')

4、完整Demo:

  1. # pygetwindow, pyautogui, pyperclip, uiautomation
  2. import time
  3. from datetime import datetime
  4. import uiautomation as auto
  5. import pyperclip
  6. import pyautogui
  7. import pygetwindow
  8. import sys
  9. import subprocess
  10. import SparkApi
  11. import AskSpark
  12. def initialization_qn():
  13. # subprocess.Popen(['千牛工作台.exe'], shell=False)
  14. # 避免使用shell=True:在某些情况下,将 shell=True 设置为 subprocess.Popen 可能导致字符编码问题。尝试删除 shell=True,并将命令和参数分开传递
  15. floating_bar = auto.WindowControl(searchDepth=1, ClassName='Qt5152QWindowToolSaveBits')
  16. if floating_bar:
  17. print("千牛已经完成初始化")
  18. else:
  19. print("打开千牛工作台")
  20. app_path = r"D:\Program Files\AliWorkbench\AliWorkbench.exe"
  21. args = [app_path, "/run:desktop"]
  22. subprocess.Popen(args, shell=False)
  23. time.sleep(30)
  24. print("打开千牛工作台接待中心")
  25. floating_bar = auto.WindowControl(searchDepth=1, ClassName='Qt5152QWindowToolSaveBits')
  26. if not floating_bar:
  27. print("千牛工作台未完成初始化,程序有问题")
  28. sys.exit()
  29. print(floating_bar.Name)
  30. floating_bar.Click(x=110, y=55, simulateMove=True)
  31. print("切换到接待中心联系中名单")
  32. rc_window = auto.WindowControl(searchDepth=1, ClassName='Qt5152QWindowIcon')
  33. rc_window.Click(x=106, y=170, simulateMove=True)
  34. # 150 290
  35. print("切换到第一个联系人")
  36. rc_window.Click(x=150, y=290, simulateMove=True)
  37. # 535 500
  38. print("移动鼠标聚焦聊天信息框")
  39. rc_window.MoveCursorToInnerPos(x=535, y=500, simulateMove=True)
  40. def get_all_siblings(start_control):
  41. # 创建一个列表来存储同级控件
  42. siblings = []
  43. # 首先获取第一个同级控件
  44. sibling = start_control.GetNextSiblingControl()
  45. while sibling is not None and sibling != auto.GetRootControl() and sibling:
  46. # 添加找到的同级控件到列表中
  47. siblings.append(sibling)
  48. # 获取下一个同级控件,直到没有更多
  49. sibling = sibling.GetNextSiblingControl()
  50. # 返回包含所有同级控件的列表
  51. return siblings
  52. def get_control_from_cursor():
  53. time.sleep(3)
  54. idea_control = auto.ControlFromCursor()
  55. if idea_control is not None and idea_control != auto.GetRootControl():
  56. print("**********")
  57. print("能够识别光标下的UI控件")
  58. auto.EnumAndLogControlAncestors(control=idea_control, showAllName=True, showPid=True)
  59. if idea_control.Name != "":
  60. print("**********")
  61. print(f"该控件有名字:{idea_control.Name}")
  62. all_children = []
  63. all_children = idea_control.GetChildren()
  64. if all_children:
  65. print("**********")
  66. print("该控件有子控件:")
  67. for item in all_children:
  68. if item:
  69. print(auto.EnumAndLogControlAncestors(control=item, showAllName=True, showPid=True))
  70. else:
  71. print("**********")
  72. print("该控件没有子控件")
  73. all_siblings = []
  74. all_siblings = get_all_siblings(idea_control)
  75. if all_siblings:
  76. print("**********")
  77. print("该控件有同级控件:")
  78. for item in all_siblings:
  79. print(auto.EnumAndLogControlAncestors(control=item, showAllName=True, showPid=True))
  80. else:
  81. print("**********")
  82. print("该控件没有同级控件")
  83. return idea_control
  84. else:
  85. print("**********")
  86. print("无法识别光标下的UI控件")
  87. return None
  88. def get_control_from_cursor2():
  89. time.sleep(3)
  90. idea_control = auto.ControlFromCursor()
  91. if idea_control:
  92. print(idea_control)
  93. print(idea_control.Name)
  94. rect = (idea_control.BoundingRectangle.left, idea_control.BoundingRectangle.top, idea_control.BoundingRectangle.right, idea_control.BoundingRectangle.bottom)
  95. rect_wh = (idea_control.BoundingRectangle.width(), idea_control.BoundingRectangle.height())
  96. print(rect)
  97. print(rect_wh)
  98. return idea_control, rect, rect_wh
  99. def get_text_area(start_x, start_y, end_x, end_y):
  100. auto.WheelUp(1)
  101. auto.WheelDown(3)
  102. auto.MoveTo(start_x, start_y)
  103. auto.PressMouse(start_x, start_y)
  104. auto.MoveTo(end_x, end_y)
  105. auto.ReleaseMouse()
  106. auto.SendKeys('{Ctrl}c')
  107. # auto.Click()
  108. text = ""
  109. text = pyperclip.paste()
  110. message = text.split('\n')
  111. # 使用filter函数过滤掉空字符串
  112. message = list(filter(lambda x: x.strip() != '', message))
  113. # # 使用列表推导过滤掉空字符串
  114. # message = [line for line in message if line.strip() != '']
  115. # 从最后一个子项开始向前遍历
  116. m_message = ""
  117. for i in range(-1, -len(message) - 1, -1):
  118. if message[i] != "已读" and message[i] != "未读" and message[i]:
  119. m_message = message[i]
  120. break
  121. if m_message:
  122. print("**********")
  123. print(m_message)
  124. return m_message
  125. if __name__ == '__main__':
  126. # idea_c = get_control_from_cursor()
  127. initialization_qn()
  128. ic, rect, rect_wh = get_control_from_cursor2()
  129. ms = get_text_area(rect[0], rect[1], rect[2], rect[3])
  130. print(f"发送问题:{ms}")
  131. q = AskSpark.ask(ms)
  132. print(f"AI回答:{q}")
  133. print("激活发送消息栏")
  134. rc_window = auto.WindowControl(searchDepth=1, ClassName='Qt5152QWindowIcon')
  135. # 1200 950
  136. rc_window.Click(x=530, y=650, simulateMove=True)
  137. auto.SendKeys(q)
  138. auto.SendKey(0x0D)
  139. # auto.SendKey('{Enter}')

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/476399
推荐阅读
相关标签
  

闽ICP备14008679号