def testNotepadCN(): automation.ShowDesktop() #打开notepad subprocess.Popen('notepad') #查找notepad, 如果name有中文,python2中要使用Unicode window = automation.WindowControl(searchDepth=1, ClassName='Notepad', SubName=u'无标题 - 记事本') #可以判断window是否存在,如果不判断,找不到window的话会抛出异常 #if window.Exists(maxSearchSeconds = 3): screenWidth, screenHeight = automation.Win32API.GetScreenSize() window.MoveWindow(screenWidth // 4, screenHeight // 4, screenWidth // 2, screenHeight // 2) #查找edit edit = automation.EditControl(searchFromControl=window) edit.DoubleClick() time.sleep(1) #python2中要使用Unicode, 模拟按键 edit.SetValue(u'hi你好') automation.SendKeys(u'{Ctrl}{End}{Enter}下面开始演示{! 4}{ENTER}', 0.2) automation.SendKeys(text) automation.SendKeys('{Enter 3}0123456789{Enter}') automation.SendKeys('ABCDEFGHIJKLMNOPQRSTUVWXYZ{Enter}') automation.SendKeys('abcdefghijklmnopqrstuvwxyz{Enter}') automation.SendKeys('`~!@#$%^&*()-_=+{ENTER}') automation.SendKeys('[]{{}{}}\\|;:\'\",<.>/?{Enter}{Ctrl}a') time.sleep(1) window.CaptureToImage('Notepad.png') automation.SendKeys( 'Image Notepad.png was captured, you will see it later.', 0.05) time.sleep(1) #查找菜单 menuItemFormat = automation.MenuItemControl(searchFromControl=window, Name=u'格式(O)') menuItemFont = automation.MenuItemControl(searchFromControl=window, Name=u'字体(F)...') windowFont = automation.WindowControl(searchFromControl=window, Name=u'字体') comboScript = automation.ComboBoxControl(searchFromControl=windowFont, AutomationId='1140') buttonOK = automation.ButtonControl(searchFromControl=windowFont, Name=u'确定') menuItemFormat.Click() menuItemFont.Click() #or automation.SendKey(automation.Keys.VK_F) comboScript.Select(u'中文 GB2312') buttonOK.Click() time.sleep(1) window.Close() time.sleep(1) # buttonNotSave = ButtonControl(searchFromControl = window, SubName = u'不保存') # buttonNotSave.Click() # or send alt+n to not save and quit # automation.SendKeys('{Alt}n') # 使用另一种查找方法 buttonNotSave = automation.FindControl( window, lambda control: control.ControlType == automation.ControlType. ButtonControl and u'不保存' in control.Name) buttonNotSave.Click() os.popen('Notepad.png')
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}{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: 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 testNotepadEN(): 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) edit = automation.EditControl(searchFromControl=window) window.SetActive() time.sleep(1) edit.DoubleClick() #use unicode string edit.SetValue(u'hi你好') automation.SendKeys(u'{Ctrl}{End}{Enter}下面开始演示{! 4}{ENTER}', 0.2) automation.SendKeys(text) automation.SendKeys('{ENTER 3}0123456789{ENTER}') automation.SendKeys('ABCDEFGHIJKLMNOPQRSTUVWXYZ{ENTER}') automation.SendKeys('abcdefghijklmnopqrstuvwxyz{ENTER}') automation.SendKeys('`~!@#$%^&*()-_=+{ENTER}') automation.SendKeys('[]{{}{}}\\|;:\'\",<.>/?{ENTER}{CTRL}a') time.sleep(1) window.CaptureToImage('Notepad.png') automation.SendKeys( 'Image Notepad.png was captured, you will see it later.', 0.05) time.sleep(1) menuItemFormat = automation.MenuItemControl(searchFromControl=window, Name='Format') menuItemFont = automation.enuItemControl(searchFromControl=window, Name='Font...') windowFont = automation.WindowControl(searchFromControl=window, Name='Font') comboScript = automation.ComboBoxControl(searchFromControl=windowFont, AutomationId='1140') buttonOK = automation.ButtonControl(searchFromControl=windowFont, Name='OK') menuItemFormat.Click() menuItemFont.Click() #or automation.SendKey(automation.Keys.VK_F) comboScript.Select('Western') buttonOK.Click() time.sleep(1) window.Close() time.sleep(1) # 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: control.ControlType == automation.ControlType. ButtonControl and 'Don\'t Save' == control.Name) buttonNotSave.Click() os.popen('Notepad.png')
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: 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)
def main(): firefoxWindow = automation.WindowControl(searchDepth=1, ClassName='MozillaWindowClass') if not firefoxWindow.Exists(0): automation.Logger.WriteLine('please run Firefox first', automation.ConsoleColor.Yellow) return firefoxWindow.ShowWindow(automation.ShowWindow.Maximize) firefoxWindow.SetActive() time.sleep(1) tab = automation.TabControl(searchFromControl=firefoxWindow) newTabButton = automation.ButtonControl(searchFromControl=tab, searchDepth=1) newTabButton.Click() edit = automation.EditControl(searchFromControl=firefoxWindow) edit.Click() automation.SendKeys( 'http://global.bing.com/?rb=0&setmkt=en-us&setlang=en-us{Enter}') time.sleep(2) searchEdit = automation.FindControl( firefoxWindow, lambda c: (isinstance(c, automation.EditControl) or isinstance(c, automation.ComboBoxControl)) and c.Name == 'Enter your search term') searchEdit.Click() automation.SendKeys( 'Python-UIAutomation-for-Windows site:github.com{Enter}', 0.05) link = automation.HyperlinkControl( searchFromControl=firefoxWindow, SubName='yinkaisheng/Python-UIAutomation-for-Windows') automation.Win32API.PressKey(automation.Keys.VK_CONTROL) link.Click() #press control to open the page in a new tab automation.Win32API.ReleaseKey(automation.Keys.VK_CONTROL) newTab = automation.TabItemControl( searchFromControl=tab, SubName='yinkaisheng/Python-UIAutomation-for-Windows') newTab.Click() starButton = automation.ButtonControl(searchFromControl=firefoxWindow, Name='Star this repository') if starButton.Exists(5): automation.GetConsoleWindow().SetActive() automation.Logger.WriteLine( 'Star Python-UIAutomation-for-Windows after 2 seconds', automation.ConsoleColor.Yellow) time.sleep(2) firefoxWindow.SetActive() time.sleep(1) starButton.Click() time.sleep(2)
def main(): # mainWindow=automation.WindowControl(searchDepth=1, ClassName='TXGuiFoundation',Name=''); # mainWindow.ShowWindow(automation.ShowWindow.Maximize) # mainWindow.SetActive() # # 群聊按钮 # groupBtn=automation.ButtonControl(searchFromControl=mainWindow); # # # # # groupBtn=automation.FindControl(searchFromControl=mainWindow); # # groupsControl=automation.ListItemControl(searchFromControl=mainWindow,Name='我的QQ群') # containControl=groupsControl.GetNextSiblingControl(); # groupItems=automation.PaneControl.GetChildren(containControl); # for grop in groupItems: # automation.ListItemControl.RightClick(grop) # automation.SendKey("{Down}"); # print(grop) # time.sleep(5) # return qqWindow = automation.WindowControl(searchDepth=1, ClassName='TXGuiFoundation') if not qqWindow.Exists(0): automation.Logger.WriteLine('please run QQ first', automation.ConsoleColor.Yellow) return qqWindow.ShowWindow(automation.ShowWindow.Maximize) qqWindow.SetActive() time.sleep(1) # 群聊窗口输入框 # edit = automation.EditControl(searchFromControl=qqWindow, Name='输入') # if !edit # print('没有找到对应的群输入框') # return # edit.Click() # automation.SendKeys('有人在吗? {Enter}') # automation.SendKeys("{Ctrl}{Enter}") # time.sleep(2) listControl = automation.ListControl(searchFromControl=qqWindow, Name='') listItem = listControl.GetChildren() # 确保群里第一个成员可见在最上面 left, top, right, bottom = listControl.BoundingRectangle while listItem[0].BoundingRectangle[1] < top: automation.Win32API.MouseClick(right - 5, top + 5) for item in listItem: if '管理员' in item.Name or '群主' in item.Name: item.Click() time.sleep(0.5) automation.SendKeys('{Down}') continue; item.RightClick() itemName = item.Name print(item) menu = automation.MenuControl(searchDepth=1, ClassName='TXGuiFoundation') menuItems = menu.GetChildren() for menuItem in menuItems: if menuItem.Name == '发送消息': menuItem.Click() time.sleep(0.5) currentTalkWin = automation.WindowControl(searchDepth=1, ClassName='TXGuiFoundation') currentTalkWin.SetActive() # time.sleep(0.5) edit = automation.EditControl(searchFromControl=currentTalkWin, Name='输入') edit.Click() text = ''' 【皮尔卡丹】家纺纯棉四件套 法式情调家纺{Enter} 【天猫89元】【23花色入】【送运险费】{Enter} 优惠券:https://shop.m.taobao.com/shop/coupon.htm?sellerId=2369408650&activityId=a5304665cb824928a35d81b73cfaa20b{Enter} 抢购链接:http://yqt.so/Re49Td{Enter} 【法国大牌】{Enter} ''' automation.SendKeys(text+' {Enter}') automation.SendKeys("{Alt}{F4}") break item.Click() time.sleep(0.5) automation.SendKeys('{Down}') time.sleep(0.5) return searchEdit = automation.FindControl(qqWindow, lambda c: (isinstance(c, automation.EditControl) or isinstance(c, automation.ComboBoxControl)) and c.Name == 'Enter your search term' ) searchEdit.Click() automation.SendKeys('Python-UIAutomation-for-Windows site:github.com{Enter}', 0.05) link = automation.HyperlinkControl(searchFromControl=qqWindow, SubName='yinkaisheng/Python-UIAutomation-for-Windows') automation.Win32API.PressKey(automation.Keys.VK_CONTROL) link.Click() # press control to open the page in a new tab automation.Win32API.ReleaseKey(automation.Keys.VK_CONTROL) newTab = automation.TabItemControl(searchFromControl=tab, SubName='yinkaisheng/Python-UIAutomation-for-Windows') newTab.Click() starButton = automation.ButtonControl(searchFromControl=qqWindow, Name='Star this repository') if starButton.Exists(5): automation.GetConsoleWindow().SetActive() automation.Logger.WriteLine('Star Python-UIAutomation-for-Windows after 2 seconds', automation.ConsoleColor.Yellow) time.sleep(2) qqWindow.SetActive() time.sleep(1) starButton.Click() time.sleep(2)
def main(): qqWindow = automation.WindowControl(searchDepth=1, ClassName='TXGuiFoundation', title='净网大师官方PC群1') if not qqWindow.Exists(0): automation.Logger.WriteLine('please run QQ first', automation.ConsoleColor.Yellow) return qqWindow.ShowWindow(automation.ShowWindow.Maximize) qqWindow.SetActive() time.sleep(1) # 群聊窗口输入框 # edit = automation.EditControl(searchFromControl=qqWindow, Name='输入') # if !edit # print('没有找到对应的群输入框') # return # edit.Click() # automation.SendKeys('有人在吗? {Enter}') # automation.SendKeys("{Ctrl}{Enter}") # time.sleep(2) listControl = automation.ListControl(searchFromControl=qqWindow, Name='') listItem = listControl.GetChildren() # 确保群里第一个成员可见在最上面 left, top, right, bottom = listControl.BoundingRectangle while listItem[0].BoundingRectangle[1] < top: automation.Win32API.MouseClick(right - 5, top + 5) for item in listItem: item.RightClick() itemName = item.Name print(item) menu = automation.MenuControl(searchDepth=1, ClassName='TXGuiFoundation') menuItems = menu.GetChildren() for menuItem in menuItems: if menuItem.Name == '发送消息': menuItem.Click() time.sleep(0.5) currentTalkWin = automation.WindowControl( searchDepth=1, ClassName='TXGuiFoundation') # currentTalkWin.ShowWindow(automation.ShowWindow.Maximize) currentTalkWin.SetActive() time.sleep(0.5) edit = automation.EditControl(searchFromControl=currentTalkWin, Name='输入') edit.Click() automation.SendKeys('有人在吗? {Enter}') automation.SendKeys("{Alt}{F4}") # automation.SendKey("{Ctrl}{Enter}") # automation.SendKey("{Alt}{F4}") break item.Click() time.sleep(0.5) automation.SendKeys('{Down}') time.sleep(0.5) return searchEdit = automation.FindControl( qqWindow, lambda c: (isinstance(c, automation.EditControl) or isinstance(c, automation.ComboBoxControl)) and c. Name == 'Enter your search term') searchEdit.Click() automation.SendKeys( 'Python-UIAutomation-for-Windows site:github.com{Enter}', 0.05) link = automation.HyperlinkControl( searchFromControl=qqWindow, SubName='yinkaisheng/Python-UIAutomation-for-Windows') automation.Win32API.PressKey(automation.Keys.VK_CONTROL) link.Click() # press control to open the page in a new tab automation.Win32API.ReleaseKey(automation.Keys.VK_CONTROL) newTab = automation.TabItemControl( searchFromControl=tab, SubName='yinkaisheng/Python-UIAutomation-for-Windows') newTab.Click() starButton = automation.ButtonControl(searchFromControl=qqWindow, Name='Star this repository') if starButton.Exists(5): automation.GetConsoleWindow().SetActive() automation.Logger.WriteLine( 'Star Python-UIAutomation-for-Windows after 2 seconds', automation.ConsoleColor.Yellow) time.sleep(2) qqWindow.SetActive() time.sleep(1) starButton.Click() time.sleep(2)