def go_to(menuItem, submenuItem): zipWindow = uiautomation.WindowControl(Name="7-Zip") uiautomation.WaitForExist(zipWindow, 5) zipWindow.MenuItemControl(RegexName=f"{menuItem}.*").Click() # Because Application changes after clicking file zipWindow = uiautomation.WindowControl(Name="7-Zip") uiautomation.WaitForExist(zipWindow, 5) zipWindow.MenuItemControl(RegexName=f"{submenuItem}.*").Click()
def close_7zip(): zipWindow = uiautomation.WindowControl(Name="7-Zip") uiautomation.WaitForExist(zipWindow, 5) zipWindow.MenuItemControl(Name="File").Click() # Because Application changes after clicking file zipWindow = uiautomation.WindowControl(Name="7-Zip") uiautomation.WaitForExist(zipWindow, 5) zipWindow.MenuItemControl(RegexName="Exit.*").Click()
def yztc_dl(self): '''判断当前是否跳转重新登录的提示''' result = automation.HyperlinkControl(Depth=11, Name='账号密码登录') if automation.WaitForExist(result, 5): result.Click() return 1 else: raise ('yztc_dl异常')
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', RegexName = '.* - 记事本') #可以判断window是否存在,如果不判断,找不到window的话会抛出异常 #if window.Exists(maxSearchSeconds = 3): if automation.WaitForExist(window, 3): automation.Logger.WriteLine("Notepad exists now") else: automation.Logger.WriteLine("Notepad does not exist after 3 seconds", automation.ConsoleColor.Yellow) 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() if automation.WaitForDisappear(window, 3): automation.Logger.WriteLine("Notepad closed") else: automation.Logger.WriteLine("Notepad still exists after 3 seconds", automation.ConsoleColor.Yellow) # buttonNotSave = ButtonControl(searchFromControl = window, SubName = '不保存') # buttonNotSave.Click() # or send alt+n to not save and quit # automation.SendKeys('{Alt}n') # 使用另一种查找方法 buttonNotSave = window.ButtonControl(Compare = lambda control, depth: '不保存' 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 yzdl(self): '''用于验证登录匹配是否有办理进度跟踪字样,如果有返回1说明登录成功''' time.sleep(5) result = automation.ButtonControl(Depth=3, Name='继续') if automation.WaitForExist(result, 5): automation.SendKeys('{Enter}') time.sleep(5) automation.SendKeys('{F6}') automation.SendKeys( 'http://yct.sh.gov.cn/portal_yct/webportal/handle_progress.do?x=12' ) automation.SendKeys('{Enter}') time.sleep(5) result = automation.CustomControl(Depth=9, Name='办理进度跟踪') if automation.WaitForExist(result, 5): return 1 else: self.restart_login = True
def portal_yct(self): '''第一次跳转到登录页''' result = automation.EditControl( Depth=11, Name='开办企业申请信息填写人需进行实名认证,系统将跳转至“一网通办”总门户进行用户注册和认证') if automation.WaitForExist(result, 5): automation.HyperlinkControl(Depth=10, Name='确定').Click() return 1 else: raise ('portal_yct异常')
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', RegexName = '.* - Notepad') #if window.Exists(maxSearchSeconds = 3): #check before using it if automation.WaitForExist(window, 3): automation.Logger.WriteLine("Notepad exists now") else: automation.Logger.WriteLine("Notepad does not exist after 3 seconds", automation.ConsoleColor.Yellow) 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() if automation.WaitForDisappear(window, 3): automation.Logger.WriteLine("Notepad closed") else: automation.Logger.WriteLine("Notepad still exists after 3 seconds", automation.ConsoleColor.Yellow) # 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 = window.ButtonControl(Compare = lambda control, depth: '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 start(self, **kwargs): timeout = kwargs.get('timeout') or 10 if kwargs.get('ClassName'): self.searchProperties['ClassName'] = kwargs.get('ClassName') if kwargs.get('RegexName'): self.searchProperties['RegexName'] = kwargs.get('RegexName') os.popen(self.desired_caps['app']) window = auto.WindowControl(searchDepth=1, **self.searchProperties) if not auto.WaitForExist(window, timeout): raise Exception('%s launch failed on Windows' % self.desired_caps['app']) window.SetActive() self.driver = window return self.driver
def firefox(): ancestor = False showAllName = False showMore = False depth = 0xFFFFFFFF wait_time_in_seconds = 5 edgeWindow = automation.WindowControl(searchDepth=1, ClassName='ApplicationFrameWindow') control = edgeWindow if automation.WaitForExist(edgeWindow, wait_time_in_seconds): automation.Logger.WriteLine("There is an Edge window open :D", automation.ConsoleColor.Green) else: automation.Logger.WriteLine("There is no Edge window open :(", automation.ConsoleColor.Red) if ancestor: # control = ControlFromCursor() control = edgeWindow if control: automation.EnumAndLogControlAncestors(control, showAllName, showMore) else: automation.Logger.Write( 'IUIAutomation return null element under cursor\n', ConsoleColor.Yellow) else: if not control: control = edgeWindow controlList = [] while control: controlList.insert(0, control) control = control.GetParentControl() if len(controlList) == 1: control = controlList[0] else: control = controlList[1] automation.EnumAndLogControl(control, depth, showAllName, showMore) automation.Logger.Log('Ends\n')
from time import sleep import uiautomation as auto print(auto.GetRootControl()) # subprocess.Popen('notepad.exe') # notepadWindow = automation.WindowControl(searchDepth=1, ClassNam='Notepad') # print(notepadWindow.Name) # notepadWindow.SetTopmost(True) # edit = notepadWindow.EditControl() # edit.SetValue('Hello') # edit.SendKeys('{Ctrl}{End}{Enter}World') subprocess.Popen('C:\Program Files (x86)\PesticideModels\TOPRice\TOPrice.exe') window = auto.WindowControl(searchDepth=1, title_re='TOPrice*') 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) window.SetActive() # windowFont = window.WindowControl(Name='File') # print(window.Name) # window.window_(title_re = u'File').Click() # # # window.SetTopmost(True) # window.ButtonControl(name="Projects").Click() # button = auto.ButtonControl(Name='Projects') # button.Click()
def WaitForExist(cls, timeout): return uiautomation.WaitForExist(WinElement._element(), timeout)
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 etozvx(text): zipWindow = uiautomation.WindowControl(Name="7-Zip") uiautomation.WaitForExist(zipWindow, 5) assert zipWindow.TextControl(RegexName=f"{text}.*").Exists( ), f"Text '{text}' does not exist in the about window or About is not opened"
def click_button(button): zipWindow = uiautomation.WindowControl(Name="7-Zip") uiautomation.WaitForExist(zipWindow, 5) zipWindow.ButtonControl(Name=button).Click()