使用工具
使用工具UISpy或者Inspect
1、定位模块
程序窗口:WindowControl()
按钮:ButtonControl()
文件显示:TextControl()
输入框:EditControl()
2、一般定位的属性
有:ClassName、Name、ProcessId、AutomationId
3、对找到句柄常用操作
Click() 点击;(# simulateMove=False 点击快,不用鼠标移动waitTime等待时间)
RighClik() 右键点击;
SendKeys() 发送字符;
SetValue() 传值,一般对EditControl用;
GetClickablePoint() 获取元素坐标点,返回元素(x,y , bool)
GetChildren() 获取元素子元素,返回列表
4、对windows程序常用操作
subprocess.Popen('Name') 用进程打开程序;
window.Close() 关闭窗口;
window.SetActive() 使用;
window.SetTopMost() 设置为顶层
window.ShowWindow(uiautomation.ShowWindow.Maximize) 窗口最大化
window.CaptureToImage('Notepad.png') 截图;
uiautomation.Win32API.PressKey(uiautomation.Keys.VK_CONTROL) 按住Ctrl键
uiautomation.Win32API.ReleaseKey(uiautomation.Keys.VK_CONTROL) 释放Ctrl键
automation.GetConsoleWindow() #return console window that runs python,打开控制台
automation.Logger.ColorfulWriteLine('\nI will open <Color=Green>Notepad</Color> and <Color=Yellow>automate</Color> it. Please wait for a while.') 控制台传值(彩色字体),普通传值用WriteLine;
automation.ShowDesktop() 显示桌面;
5、句柄的抓取
直接运行automation模块枚举窗口时,支持下列参数(从doc窗口运行automation.py程序 ):
-t intValue 延迟枚举时间,单位秒
-r 从树的根部枚举,如果不指定,从当前窗口枚举
-d intValue 枚举控件树的的深度,如果不指定,枚举整个树
-f 从焦点控件枚举,如果不指定,从当前窗口枚举
-c 从光标下的控件枚举,如果不指定,从当前窗口枚举
-a 获取光标下控件及其所有父控件
-n 显示控件的完整Name, 如果不指定,只显示前30个字符
-m 显示控件更多属性,默认只显示控件的四个属性
automation.py c –t3 // 3秒后枚举当前窗口所有控件 automation.py c –d2 –t3 // 3秒后枚举当前窗口前三层控件 automation.py c –r –d1 –t0 -n // 0秒后从根部枚举前两层控件,并显示控件完整名称 automation.py c –c –t3 // 3秒后显示鼠标光标下面的控件信息
#打开记事本输入文字 import subprocess import uiautomation as automation print(automation.GetRootControl()) #打开软件 subprocess.Popen('notepad.exe') # 定位 notepadWindow = automation.WindowControl(searchDepth=1, ClassName='Notepad') print(notepadWindow.Name) #设置为顶层 notepadWindow.SetTopmost(True) edit = notepadWindow.EditControl() # 发送文字 edit.SendKeys('哈哈')
#例子1:qq点赞 import subprocess import uiautomation as automation import random import time import win32api import win32con print(automation.GetRootControl()) my_qq = automation.WindowControl(searchDepth=1, ClassName='TXGuiFoundation', Name="QQ") print(my_qq.Name) my_qq.SetTopmost(True) # edit = my_qq.TextControl(Name="联系人") child1 = my_qq.GetChildren()[1] child2 = child1.GetChildren()[0] child3 = child2.GetChildren()[3] child4 = child3.GetChildren()[1] # simulateMove=False 点击快,不用鼠标移动 child4.SetFocus() child4.Click(simulateMove=False) # 我的资料 my_info = automation.WindowControl(searchDepth=1, ClassName='TXGuiFoundation', Name="我的资料") # my_info.SetTopmost(True) info = my_info.GetChildren() chil5 = info[1] chil6 = chil5.GetChildren()[0] chil6 = chil6.GetChildren()[0] chil6 = chil6.GetChildren()[0] chil6 = chil6.GetChildren()[0] chil6 = chil6.GetChildren()[0] chil6 = chil6.GetChildren()[2] chil6.SetFocus() chil6.Click(simulateMove=False) print(chil6) # 点赞 click_zan = automation.PaneControl(Name="谁赞过我") click_zan.SetTopmost(True) child7 = click_zan.GetChildren()[0] child7 = child7.GetChildren()[0] child7 = child7.GetChildren()[0] child7 = child7.GetChildren() print("chil7=%s" % child7) for elem in child7: try: child8 = elem.GetChildren()[1] # print("child8 1=%s" % child8) child8 = child8.GetChildren()[3] # print("child8 2=%s" % child8) if child8: child8.SetFocus() for i in range(50): point = child8.GetClickablePoint() time.sleep(random.uniform(0.1, 0.2)) # 鼠标左键的点击 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, point[0], point[1], 0, 0) # 按下 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, point[0], point[1], 0, 0) # 抬起 child8.Click(simulateMove=False) except Exception as e: print(e)