def testNotepadCN(): consoleWindow = automation.GetConsoleWindow() consoleWindow.SetActive() automation.Logger.ColorfulWriteLine( '\nI will open <Color=Green>Notepad</Color> and <Color=Yellow>automate</Color> it. Please wait for a while.' ) time.sleep(2) automation.ShowDesktop() #打开notepad subprocess.Popen('notepad') #查找notepad, 如果name有中文,python2中要使用Unicode window = automation.WindowControl(searchDepth=1, ClassName='Notepad', SubName='无标题 - 记事本') #可以判断window是否存在,如果不判断,找不到window的话会抛出异常 #if window.Exists(maxSearchSeconds = 3): screenWidth, screenHeight = automation.Win32API.GetScreenSize() window.MoveWindow(screenWidth // 4, screenHeight // 4, screenWidth // 2, screenHeight // 2) window.SetActive() #查找edit edit = automation.EditControl( searchFromControl=window) #or edit = window.EditControl() edit.Click(waitTime=0) #python2中要使用Unicode, 模拟按键 edit.SetValue('hi你好') edit.SendKeys('{Ctrl}{End}{Enter}下面开始演示{! 4}{ENTER}', 0.2, 0) edit.SendKeys(text) edit.SendKeys('{Enter 3}0123456789{Enter}', waitTime=0) edit.SendKeys('ABCDEFGHIJKLMNOPQRSTUVWXYZ{ENTER}', waitTime=0) edit.SendKeys('abcdefghijklmnopqrstuvwxyz{ENTER}', waitTime=0) edit.SendKeys('`~!@#$%^&*()-_=+{ENTER}', waitTime=0) edit.SendKeys('[]{{}{}}\\|;:\'\",<.>/?{ENTER}', waitTime=0) edit.SendKeys( '™®①②③④⑤⑥⑦⑧⑨⑩§№☆★○●◎◇◆□℃‰€■△▲※→←↑↓〓¤°#&@\^_―♂♀{ENTER}{CTRL}a') window.CaptureToImage('Notepad.png') edit.SendKeys('Image Notepad.png was captured, you will see it later.', 0.05) #查找菜单 window.MenuItemControl(Name='格式(O)').Click() window.MenuItemControl(Name='字体(F)...').Click() windowFont = window.WindowControl(Name='字体') windowFont.ComboBoxControl(AutomationId='1140').Select('中文 GB2312') windowFont.ButtonControl(Name='确定').Click() window.Close() # buttonNotSave = ButtonControl(searchFromControl = window, SubName = '不保存') # buttonNotSave.Click() # or send alt+n to not save and quit # automation.SendKeys('{Alt}n') # 使用另一种查找方法 buttonNotSave = automation.FindControl( window, lambda control, depth: control.ControlType == automation. ControlType.ButtonControl and '不保存' in control.Name) buttonNotSave.Click() subprocess.Popen('Notepad.png', shell=True) time.sleep(2) consoleWindow.SetActive() automation.Logger.WriteLine('script exits', automation.ConsoleColor.Cyan) time.sleep(2)
def main(): auto.ShowDesktop() subprocess.Popen('notepad') time.sleep(1) note = auto.WindowControl(searchDepth=1, ClassName='Notepad') note.SetActive() note.SetTopmost() transformNote = note.GetTransformPattern() transformNote.Move(400, 0) transformNote.Resize(400, 300) edit = note.EditControl() edit.SendKeys('{Ctrl}{End}{Enter 2}I\'m a topmost window!!!\nI cover other windows.') subprocess.Popen('mmc.exe devmgmt.msc') mmcWindow = auto.WindowControl(searchDepth=1, ClassName='MMCMainFrame') mmcWindow.SetActive() transformMmc = mmcWindow.GetTransformPattern() transformMmc.Move(100, 100) transformMmc.Resize(400, 300) time.sleep(1) auto.DragDrop(160, 110, 900, 110, 0.2) auto.DragDrop(900, 110, 160, 110, 0.2) mmcWindow.SendKeys('{Alt}f', waitTime=1) mmcWindow.SendKeys('X', charMode=False) # or mmcWindow.SendKey(auto.Keys.VK_X) edit.SendKeys('{Ctrl}{End}{Enter 2}You close me.') vp = edit.GetValuePattern() if vp: print('current text:', vp.Value) auto.GetConsoleWindow().SetActive()
def testNotepadEN(): consoleWindow = automation.GetConsoleWindow() consoleWindow.SetActive() automation.Logger.ColorfulWriteLine( '\nI will open <Color=Green>Notepad</Color> and <Color=Yellow>automate</Color> it. Please wait for a while.' ) time.sleep(2) automation.ShowDesktop() subprocess.Popen('notepad') #search notepad window, if searchFromControl is None, search from RootControl #searchDepth = 1 makes searching faster, only searches RootControl's children, not children's children window = automation.WindowControl(searchDepth=1, ClassName='Notepad', SubName='Untitled - Notepad') #if window.Exists(maxSearchSeconds = 3): #check before using it #pass screenWidth, screenHeight = automation.Win32API.GetScreenSize() window.MoveWindow(screenWidth // 4, screenHeight // 4, screenWidth // 2, screenHeight // 2) window.SetActive() edit = automation.EditControl( searchFromControl=window) #or edit = window.EditControl() edit.Click(waitTime=0) edit.SetValue('hi你好') edit.SendKeys('{Ctrl}{End}{Enter}下面开始演示{! 4}{ENTER}', 0.2, 0) edit.SendKeys(text) edit.SendKeys('{Enter 3}0123456789{Enter}', waitTime=0) edit.SendKeys('ABCDEFGHIJKLMNOPQRSTUVWXYZ{Enter}', waitTime=0) edit.SendKeys('abcdefghijklmnopqrstuvwxyz{Enter}', waitTime=0) edit.SendKeys('`~!@#$%^&*()-_=+{Enter}', waitTime=0) edit.SendKeys('[]{{}{}}\\|;:\'\",<.>/?{Enter}{Ctrl}a') window.CaptureToImage('Notepad.png') edit.SendKeys('Image Notepad.png was captured, you will see it later.', 0.05) #find menu window.MenuItemControl(Name='Format').Click() window.MenuItemControl(Name='Font...').Click() windowFont = window.WindowControl(Name='Font') windowFont.ComboBoxControl(AutomationId='1140').Select('Western') windowFont.ButtonControl(Name='OK').Click() window.Close() # buttonNotSave = ButtonControl(searchFromControl = window, Name = 'Don\'t Save') # buttonNotSave.Click() # or send alt+n to not save and quit # automation.SendKeys('{Alt}n') # another way to find the button using lambda buttonNotSave = automation.FindControl( window, lambda control, depth: control.ControlType == automation. ControlType.ButtonControl and 'Don\'t Save' == control.Name) buttonNotSave.Click() subprocess.Popen('Notepad.png', shell=True) time.sleep(2) consoleWindow.SetActive() automation.Logger.WriteLine('script exits', automation.ConsoleColor.Cyan) time.sleep(2)
安装pip install uiautomation后,在Python的Scripts(比如C:\Python37\Scripts)目录中会有一个文件automation.py, 或者使用源码根目录里的automation.py。automation.py是用来枚举控件树结构的一个脚本。 """ # 由于Windows自带的UI Automation是一个dll, 提供的接口是以C++来描述的, 所以作者在打包接口到Python时使用了很多c type # 下面以作者Github上的一个demo做例子 import time import uiautomation as auto import subprocess if __name__ == '__main__': auto.uiautomation.SetGlobalSearchTimeout(15) # 设置全局搜索超时时间 auto.ShowDesktop() # 等于Win+D subprocess.Popen('notepad') # 等于Win+R 再输入notepad回车 time.sleep(1) ''' 查找ClassName为Notepad的Window控件, 查找深度1 1. 注意查找深度是以当控件为0而定义的, 一般设置为1的话就表示搜索到当前控件的下一层 2. GUI最顶层的控件是Windows桌面, 而一般来说一个程序的顶层控件都是一个WindowControl 3. 在Windows的UIAutomation中, 所能够控制的控件一定是属于某种Control类型的, 比如一个Window就属于WindowControl WindowControl里面包含的其它控件, 比如notepad的Menu,就属于MenuBarControl ''' note = auto.WindowControl(searchDepth=1, ClassName="Notepad") note.SetActive() # edit = note.EditControl() # 查找notepad window下的第一个edit控件, 不指定任何条件的话默认就是返回找到的第一个控件 ''' 而不同的Control类型会支持不同的操作, UIAutomation中, 把一些相同的操作包装成为了Pattern, 一种Control能够支持不同的Pattern,
import os, sys, time import subprocess import uiautomation as automation from pywinauto import application automation.ShowDesktop() # 打开cas subprocess.Popen(args='D:\Release-4396\Higemi.exe',cwd=r"D:\Release-4396") # # 查找cas cas = automation.WindowControl(searchDepth=1, AutomationId='myMainWindow', Name='Login') # 可以判断window是否存在,如果不判断,找不到window的话会抛出异常 if automation.WaitForExist(cas, 3): automation.Logger.WriteLine("CAS exists now",logFile="cas.txt") else: automation.Logger.WriteLine("CAS does not exist after 3 seconds",logFile="cas.txt") cas.SetActive() # # 登录 login = automation.ButtonControl(searchFromControl=cas,AutomationId ="btn_login") # login=cas.ButtonControl(AutomationId ="btn_login") login.Click() # 查找病例 patientManager = automation.WindowControl(Name = 'PatientManager',AutomationId='myMainWindow') patientManager.SetActive() patient = automation.TextControl(searchFromControl=patientManager,Name = 'LI QIANG',ClassName="TextBlock",foundIndex=1) patient.Click() # # 进入三维重建 _3dRebuidBtn = patientManager.ButtonControl(AutomationId="_3dRebuidBtn") _3dRebuidBtn.Click() time.sleep(10) lung = patientManager.ImageControl(AutomationId="lung") lung.Click() # 设置窗宽窗位
def ShowDesktop(self): ''' 显示桌面 ''' auto.ShowDesktop()
def demo(): consoleWindow = auto.GetConsoleWindow() consoleWindow.SetActive() auto.Logger.ColorfullyWriteLine( '\nI will open <Color=Green>Notepad</Color> and <Color=Yellow>automate</Color> it. Please wait for a while.' ) time.sleep(2) auto.ShowDesktop() # 打开notepad subprocess.Popen('notepad') # 查找notepad, 如果name有中文,python2中要使用Unicode notepad = auto.WindowControl(searchDepth=1, ClassName='Notepad', RegexName='.* - 记事本') # 可以判断window是否存在,如果不判断,找不到window的话会抛出异常 # if window.Exists(maxSearchSeconds = 3): if auto.WaitForExist(notepad, 3): auto.Logger.WriteLine("Notepad exists now") else: auto.Logger.WriteLine("Notepad does not exist after 3 seconds", auto.ConsoleColor.Yellow) screenWidth, screenHeight = auto.GetScreenSize() notepad.MoveWindow(screenWidth // 4, screenHeight // 4, screenWidth // 2, screenHeight // 2) notepad.SetActive() # 查找edit # or edit = window.EditControl() edit = auto.EditControl(searchFromControl=notepad) edit.Click(waitTime=0) # python2中要使用Unicode, 模拟按键 edit.GetValuePattern().SetValue('hi你好') edit.SendKeys('{Ctrl}{End}{Enter}下面开始演示{! 4}{ENTER}', 0.2, 0) edit.SendKeys(text) edit.SendKeys('{Enter 3}0123456789{Enter}', waitTime=0) edit.SendKeys('ABCDEFGHIJKLMNOPQRSTUVWXYZ{ENTER}', waitTime=0) edit.SendKeys('abcdefghijklmnopqrstuvwxyz{ENTER}', waitTime=0) edit.SendKeys('`~!@#$%^&*()-_=+{ENTER}', waitTime=0) edit.SendKeys('[]{{}{}}\\|;:\'\",<.>/?{ENTER}', waitTime=0) edit.SendKeys( '™®①②③④⑤⑥⑦⑧⑨⑩§№☆★○●◎◇◆□℃‰€■△▲※→←↑↓〓¤°#&@\^_―♂♀{ENTER}{CTRL}a') notepad.CaptureToImage('Notepad.png') edit.SendKeys('Image Notepad.png was captured, you will see it later.', 0.05) # 查找菜单 notepad.MenuItemControl(Name='格式(O)').Click() notepad.MenuItemControl(Name='字体(F)...').Click() windowFont = notepad.WindowControl(Name='字体') listItem = windowFont.ListControl( searchDepth=2, AutomationId='1000').ListItemControl(Name='微软雅黑') if listItem.Exists(2): listItem.GetScrollItemPattern().ScrollIntoView() listItem.Click() windowFont.ComboBoxControl(AutomationId='1140').Select('中文 GB2312') windowFont.ButtonControl(Name='确定').Click() notepad.GetWindowPattern().Close() if auto.WaitForDisappear(notepad, 3): auto.Logger.WriteLine("Notepad closed") else: auto.Logger.WriteLine("Notepad still exists after 3 seconds", auto.ConsoleColor.Yellow) # buttonNotSave = ButtonControl(searchFromControl = window, SubName = '不保存') # buttonNotSave.Click() # or send alt+n to not save and quit # auto.SendKeys('{Alt}n') # 使用另一种查找方法 buttonNotSave = notepad.ButtonControl( Compare=lambda control, depth: '不保存' in control.Name or '否' in control. Name) buttonNotSave.Click() subprocess.Popen('Notepad.png', shell=True) time.sleep(2) consoleWindow.SetActive() auto.Logger.WriteLine('script exits', auto.ConsoleColor.Cyan) time.sleep(2)
def testNotepadEN(): consoleWindow = auto.GetConsoleWindow() consoleWindow.SetActive() auto.Logger.ColorfullyWriteLine('\nI will open <Color=Green>Notepad</Color> and <Color=Yellow>automate</Color> it. Please wait for a while.') time.sleep(2) auto.ShowDesktop() subprocess.Popen('notepad') # search notepad window, if searchFromControl is None, search from RootControl # searchDepth = 1 makes searching faster, only searches RootControl's children, not children's children window = auto.WindowControl(searchDepth=1, ClassName='Notepad', RegexName='.* - Notepad') # if window.Exists(maxSearchSeconds = 3): #check before using it if auto.WaitForExist(window, 3): auto.Logger.WriteLine("Notepad exists now") else: auto.Logger.WriteLine("Notepad does not exist after 3 seconds", auto.ConsoleColor.Yellow) screenWidth, screenHeight = auto.GetScreenSize() window.MoveWindow(screenWidth // 4, screenHeight // 4, screenWidth // 2, screenHeight // 2) window.SetActive() edit = auto.EditControl(searchFromControl=window) # or edit = window.EditControl() edit.Click(waitTime=0) edit.GetValuePattern().SetValue('hi你好') edit.SendKeys('{Ctrl}{End}{Enter}下面开始演示{! 4}{ENTER}', 0.2, 0) edit.SendKeys(text) edit.SendKeys('{Enter 3}0123456789{Enter}', waitTime=0) edit.SendKeys('ABCDEFGHIJKLMNOPQRSTUVWXYZ{Enter}', waitTime=0) edit.SendKeys('abcdefghijklmnopqrstuvwxyz{Enter}', waitTime=0) edit.SendKeys('`~!@#$%^&*()-_=+{Enter}', waitTime=0) edit.SendKeys('[]{{}{}}\\|;:\'\",<.>/?{Enter}{Ctrl}a') window.CaptureToImage('Notepad.png') edit.SendKeys('Image Notepad.png was captured, you will see it later.', 0.05) # find menu window.MenuItemControl(Name='Format').Click() window.MenuItemControl(Name='Font...').Click() windowFont = window.WindowControl(Name='Font') comboIndex = 0 selectIndex = 2 def select3rd(itemText: str): nonlocal comboIndex if comboIndex == selectIndex: return True comboIndex = comboIndex + 1 combo = windowFont.ComboBoxControl(AutomationId='1140') combo.Select(condition=select3rd) combo.Select('Western') windowFont.ButtonControl(Name='OK').Click() window.Close() if auto.WaitForDisappear(window, 3): auto.Logger.WriteLine("Notepad closed") else: auto.Logger.WriteLine("Notepad still exists after 3 seconds", auto.ConsoleColor.Yellow) # buttonNotSave = ButtonControl(searchFromControl = window, Name = 'Don\'t Save') # buttonNotSave.Click() # or send alt+n to not save and quit # auto.SendKeys('{Alt}n') # another way to find the button using lambda buttonNotSave = window.ButtonControl(Compare=lambda control, depth: 'Don\'t Save' in control.Name or 'No' in control.Name) buttonNotSave.Click() subprocess.Popen('Notepad.png', shell=True) time.sleep(2) consoleWindow.SetActive() auto.Logger.WriteLine('script exits', auto.ConsoleColor.Cyan) time.sleep(2)