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 drag_drop(self, x1, y1, x2, y2): try: auto.DragDrop(x1, y1, x2, y2) logger.info("UiElement从x_{}_,y_{}_,拖动到x_{}_,y_{}_成功".format( x1, y1, x2, y2)) except: logger.exception("UiElement从x_{}_,y_{}_,拖动到x_{}_,y_{}_成功".format( x1, y1, x2, y2))
def rigthSide(self): try: splitter = auto.ThumbControl( ClassName='GridSplitter').GetClickablePoint() if splitter[0] < 400: auto.DragDrop(splitter[0], splitter[1], 600, splitter[1], 1) self.__log.info("左侧栏滑倒最右端") except: pass
def LongClick(self, x, y, duration): self.JudgeSize() Left = self.root.BoundingRectangle[0] Top = self.root.BoundingRectangle[1] Width = self.root.BoundingRectangle[2] - self.root.BoundingRectangle[0] Height = self.root.BoundingRectangle[3] - self.root.BoundingRectangle[1] x = Left + Width * x y = Top + Height * y UIAuto.MAX_MOVE_SECOND = duration * 10 UIAuto.DragDrop(int(x), int(y), int(x), int(y)) return True
def AutoSetWLWB(self): surgical_WBWL = self.find(**self.surgical_WBWL) ImageControl2D = self.find(searchFromControl=surgical_WBWL, **self.ImageControl2D) imageBefor = self.CaptureImage(ImageControl2D, "../screenshots/imageBefor.png") auto.DragDrop(1500, 356, 1600, 346) imageAfter = self.CaptureImage(ImageControl2D, "../screenshots/imageAfter.png") image = [imageBefor, imageAfter] return image
def Swipe(self, x1, y1, x2, y2, duration): self.JudgeSize() Left = self.root.BoundingRectangle[0] Top = self.root.BoundingRectangle[1] Width = self.root.BoundingRectangle[2] - self.root.BoundingRectangle[0] Height = self.root.BoundingRectangle[3] - self.root.BoundingRectangle[1] x1 = Left + Width * x1 y1 = Top + Height * y1 x2 = Left + Width * x2 y2 = Top + Height * y2 UIAuto.MAX_MOVE_SECOND = duration * 10 UIAuto.DragDrop(int(x1), int(y1), int(x2), int(y2)) return True
def Swipe(self, x1, y1, x2, y2, duration, **kwargs): self.JudgeSize() Left = self.root.BoundingRectangle[0] Top = self.root.BoundingRectangle[1] Width = self.root.BoundingRectangle[2] - self.root.BoundingRectangle[0] Height = self.root.BoundingRectangle[3] - self.root.BoundingRectangle[1] x1 = int(Left + Width * x1) # 比例换算 y1 = int(Top + Height * y1) x2 = int(Left + Width * x2) y2 = int(Top + Height * y2) UIAuto.MAX_MOVE_SECOND = duration * 10 # 同步到跟UIAutomation库的时间设定一样 UIAuto.DragDrop(int(x1), int(y1), int(x2), int(y2)) return True
def DemoCN(): """for Chinese language""" thisWindow = auto.GetConsoleWindow() auto.Logger.ColorfullyWrite( '我将运行<Color=Cyan>cmd</Color>并设置它的<Color=Cyan>屏幕缓冲区</Color>使<Color=Cyan>cmd</Color>一行能容纳很多字符\n\n' ) time.sleep(3) auto.SendKeys('{Win}r') while not isinstance(auto.GetFocusedControl(), auto.EditControl): time.sleep(1) auto.SendKeys('cmd{Enter}') cmdWindow = auto.WindowControl(RegexName='.+cmd.exe') cmdWindow.TitleBarControl().RightClick() auto.SendKey(auto.Keys.VK_P) optionWindow = cmdWindow.WindowControl(SubName='属性') optionWindow.TabItemControl(SubName='选项').Click() optionTab = optionWindow.PaneControl(SubName='选项') checkBox = optionTab.CheckBoxControl(AutomationId='103') if checkBox.GetTogglePattern().ToggleState != auto.ToggleState.On: checkBox.Click() checkBox = optionTab.CheckBoxControl(AutomationId='104') if checkBox.GetTogglePattern().ToggleState != auto.ToggleState.On: checkBox.Click() optionWindow.TabItemControl(SubName='布局').Click() layoutTab = optionWindow.PaneControl(SubName='布局') layoutTab.EditControl(AutomationId='301').GetValuePattern().SetValue('300') layoutTab.EditControl( AutomationId='303').GetValuePattern().SetValue('3000') layoutTab.EditControl(AutomationId='305').GetValuePattern().SetValue('140') layoutTab.EditControl(AutomationId='307').GetValuePattern().SetValue('30') optionWindow.ButtonControl(AutomationId='1').Click() cmdWindow.SetActive() rect = cmdWindow.BoundingRectangle auto.DragDrop(rect.left + 50, rect.top + 10, 50, 30) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '我将运行<Color=Cyan>记事本</Color>并输入<Color=Cyan>Hello!!!</Color>\n\n') time.sleep(3) subprocess.Popen('notepad') notepadWindow = auto.WindowControl(searchDepth=1, ClassName='Notepad') cx, cy = auto.GetScreenSize() notepadWindow.MoveWindow(cx // 2, 20, cx // 2, cy // 2) time.sleep(0.5) notepadWindow.EditControl().SendKeys('Hello!!!', 0.05) time.sleep(1) dir = os.path.dirname(__file__) scriptPath = os.path.abspath(os.path.join(dir, '..\\automation.py')) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '运行"<Color=Cyan>automation.py -h</Color>"显示帮助\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -h'.format(scriptPath) + '{Enter}', 0.05) time.sleep(3) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '运行"<Color=Cyan>automation.py -r -d1</Color>"显示所有顶层窗口, 即桌面的子窗口\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -r -d1 -t0'.format(scriptPath) + '{Enter}', 0.05) time.sleep(3) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '运行"<Color=Cyan>automation.py -c</Color>"显示鼠标光标下的控件\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -c -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.MoveCursorToMyCenter() time.sleep(3) cmdWindow.SetActive(waitTime=2) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '运行"<Color=Cyan>automation.py -a</Color>"显示鼠标光标下的控件和它的所有父控件\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -a -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.MoveCursorToMyCenter() time.sleep(3) cmdWindow.SetActive(waitTime=2) thisWindow.SetActive() auto.Logger.ColorfullyWrite( '运行"<Color=Cyan>automation.py</Color>"显示当前激活窗口和它的所有子控件\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.EditControl().Click() time.sleep(3) cmdWindow.SetActive(waitTime=2) time.sleep(3) thisWindow.SetActive() auto.Logger.WriteLine('演示结束,按Enter退出', auto.ConsoleColor.Green) input()
def DemoEN(): """for other language""" thisWindow = auto.GetConsoleWindow() auto.Logger.ColorfullyWrite('I will run <Color=Cyan>cmd</Color>\n\n') time.sleep(3) auto.SendKeys('{Win}r') while not isinstance(auto.GetFocusedControl(), auto.EditControl): time.sleep(1) auto.SendKeys('cmd{Enter}') cmdWindow = auto.WindowControl(SubName='cmd.exe') rect = cmdWindow.BoundingRectangle auto.DragDrop(rect.left + 50, rect.top + 10, 50, 10) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'I will run <Color=Cyan>Notepad</Color> and type <Color=Cyan>Hello!!!</Color>\n\n' ) time.sleep(3) subprocess.Popen('notepad') notepadWindow = auto.WindowControl(searchDepth=1, ClassName='Notepad') cx, cy = auto.GetScreenSize() notepadWindow.MoveWindow(cx // 2, 20, cx // 2, cy // 2) time.sleep(0.5) notepadWindow.EditControl().SendKeys('Hello!!!', 0.05) time.sleep(1) dir = os.path.dirname(__file__) scriptPath = os.path.abspath(os.path.join(dir, '..\\automation.py')) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'run "<Color=Cyan>automation.py -h</Color>" to display the help\n\n') time.sleep(3) cmdWindow.SendKeys('"{}" -h'.format(scriptPath) + '{Enter}', 0.05) time.sleep(3) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'run "<Color=Cyan>automation.py -r -d1</Color>" to display the top level windows, desktop\'s children\n\n' ) time.sleep(3) cmdWindow.SendKeys('"{}" -r -d1 -t0'.format(scriptPath) + '{Enter}', 0.05) time.sleep(3) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'run "<Color=Cyan>automation.py -c</Color>" to display the control under mouse cursor\n\n' ) time.sleep(3) cmdWindow.SendKeys('"{}" -c -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.MoveCursorToMyCenter() time.sleep(3) cmdWindow.SetActive(waitTime=2) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'run "<Color=Cyan>automation.py -a</Color>" to display the control under mouse cursor and its ancestors\n\n' ) time.sleep(3) cmdWindow.SendKeys('"{}" -a -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.MoveCursorToMyCenter() time.sleep(3) cmdWindow.SetActive(waitTime=2) thisWindow.SetActive() auto.Logger.ColorfullyWrite( 'run "<Color=Cyan>automation.py</Color>" to display the active window\n\n' ) time.sleep(3) cmdWindow.SendKeys('"{}" -t3'.format(scriptPath) + '{Enter}', 0.05) notepadWindow.SetActive() notepadWindow.EditControl().Click() time.sleep(3) cmdWindow.SetActive(waitTime=2) time.sleep(3) thisWindow.SetActive() auto.Logger.WriteLine('press Enter to exit', auto.ConsoleColor.Green) input()
def main(): sw, sh = auto.GetScreenSize() cmdWindow = auto.GetConsoleWindow() if cmdWindow: cmdTransformPattern = cmdWindow.GetTransformPattern() cmdTransformPattern.Move(sw // 2, 0) cmdTransformPattern.Resize(sw // 2, sh * 3 // 4) subprocess.Popen('mmc.exe devmgmt.msc') mmcWindow = auto.WindowControl(searchDepth=1, ClassName='MMCMainFrame') mmcTransformPattern = mmcWindow.GetTransformPattern() mmcTransformPattern.Move(0, 0) mmcTransformPattern.Resize(sw // 2, sh * 3 // 4) tree = mmcWindow.TreeControl() for item, depth in auto.WalkControl(tree, includeTop=True): if isinstance( item, auto.TreeItemControl ): #or item.ControlType == auto.ControlType.TreeItemControl item.GetSelectionItemPattern().Select(waitTime=0.05) pattern = item.GetExpandCollapsePattern() if pattern.ExpandCollapseState == auto.ExpandCollapseState.Collapsed: pattern.Expand(waitTime=0.05) auto.Logger.WriteLine(' ' * (depth - 1) * 4 + item.Name, auto.ConsoleColor.Green) if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll by <Color=Cyan>SetScrollPercent</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) treeScrollPattern = tree.GetScrollPattern() treeScrollPattern.SetScrollPercent(auto.ScrollPattern.NoScrollValue, 0) treeScrollPattern.SetScrollPercent(auto.ScrollPattern.NoScrollValue, 100) if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll to top by SendKeys <Color=Cyan>Ctrl+Home</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) tree.SendKeys('{Ctrl}{Home}', waitTime=1) if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll to bottom by SendKeys <Color=Cyan>Ctrl+End</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) tree.SendKeys('{Ctrl}{End}', waitTime=1) if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll to top by <Color=Cyan>WheelUp</Color>') cmdWindow.SetActive(waitTime=1) print(tree.NativeWindowHandle, tree.Element, len(tree.GetChildren())) # before expand, tree has no scrollbar. after expand, tree has a scrollbar. # need to Refind on some PCs before find ScrollBarControl from tree # maybe the old element has no scrollbar info tree.Refind() print(tree.NativeWindowHandle, tree.Element, len(tree.GetChildren())) vScrollBar = tree.ScrollBarControl( AutomationId='NonClientVerticalScrollBar') rangeValuePattern = vScrollBar.GetRangeValuePattern() vScrollBarRect = vScrollBar.BoundingRectangle thumb = vScrollBar.ThumbControl() while True: vPercent = treeScrollPattern.VerticalScrollPercent vPercent2 = rangeValuePattern.Value print('ScrollPattern.VerticalScrollPercent', vPercent) print('ValuePattern.Value', vPercent2) if vPercent2 > 0: tree.WheelUp(waitTime=0.05) else: break if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll to bottom by <Color=Cyan>WheelDown</Color>') cmdWindow.SetActive(waitTime=1) while True: vPercent = treeScrollPattern.VerticalScrollPercent vPercent2 = rangeValuePattern.Value print('ScrollPattern.VerticalScrollPercent', vPercent) print('ValuePattern.Value', vPercent2) if vPercent2 < 100: tree.WheelDown(waitTime=0.05) else: break if cmdWindow: auto.Logger.ColorfullyWriteLine( 'Scroll by <Color=Cyan>DragDrop</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) x, y = thumb.MoveCursorToMyCenter() auto.DragDrop(x, y, x, vScrollBarRect.top, waitTime=1) x, y = thumb.MoveCursorToMyCenter() auto.DragDrop(x, y, x, vScrollBarRect.bottom) mmcWindow.GetWindowPattern().Close() if cmdWindow: cmdWindow.SetActive() auto.Logger.Write('\nPress any key to exit', auto.ConsoleColor.Cyan) import msvcrt while not msvcrt.kbhit(): pass
def main(): cmdWindow = automation.GetConsoleWindow() subprocess.Popen('mmc.exe devmgmt.msc') time.sleep(1) mmcWindow = automation.WindowControl(searchDepth=1, ClassName='MMCMainFrame') tree = mmcWindow.TreeControl() for item, depth in automation.WalkControl(tree, includeTop=True): if isinstance( item, automation.TreeItemControl ): #or item.ControlType == automation.ControlType.TreeItemControl item.Select() if automation.ExpandCollapseState.Collapsed == item.CurrentExpandCollapseState( ): item.Expand(0) automation.Logger.WriteLine(' ' * (depth - 1) * 4 + item.Name, automation.ConsoleColor.Green) time.sleep(0.1) if cmdWindow: automation.Logger.ColorfulWriteLine( 'Scroll to top by SendKeys <Color=Cyan>Ctrl+Home</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) tree.SendKeys('{Ctrl}{Home}', waitTime=1) if cmdWindow: automation.Logger.ColorfulWriteLine( 'Scroll to bottom by SendKeys <Color=Cyan>Ctrl+End</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) tree.SendKeys('{Ctrl}{End}', waitTime=1) if cmdWindow: automation.Logger.ColorfulWriteLine( 'Scroll to top by <Color=Cyan>WheelUp</Color>') cmdWindow.SetActive(waitTime=1) print(tree.Handle, tree.Element, len(tree.GetChildren())) # before expand, tree has no scrollbar. after expand, tree has a scrollbar. # need to Refind on some PCs before find ScrollBarControl from tree # maybe the old element has no scrollbar info tree.Refind() print(tree.Handle, tree.Element, len(tree.GetChildren())) vScrollBar = tree.ScrollBarControl( AutomationId='NonClientVerticalScrollBar') vScrollBarRect = vScrollBar.BoundingRectangle thumb = vScrollBar.ThumbControl() while True: vPercent = tree.CurrentVerticalScrollPercent() vPercent2 = vScrollBar.RangeValuePatternCurrentValue() print('TreeControl.CurrentVerticalScrollPercent', vPercent) print('ScrollBarControl.RangeValuePatternCurrentValue', vPercent2) if vPercent2 > 0: tree.WheelUp(waitTime=0.1) else: break if cmdWindow: automation.Logger.ColorfulWriteLine( 'Scroll to bottom by <Color=Cyan>WheelDown</Color>') cmdWindow.SetActive(waitTime=1) while True: vPercent = tree.CurrentVerticalScrollPercent() vPercent2 = vScrollBar.RangeValuePatternCurrentValue() print('TreeControl.CurrentVerticalScrollPercent', vPercent) print('ScrollBarControl.RangeValuePatternCurrentValue', vPercent2) if vPercent2 < 100: tree.WheelDown(waitTime=0.1) else: break if cmdWindow: automation.Logger.ColorfulWriteLine( 'Scroll by <Color=Cyan>DragDrop</Color>') cmdWindow.SetActive(waitTime=1) mmcWindow.SetActive(waitTime=1) x, y = thumb.MoveCursorToMyCenter() automation.DragDrop(x, y, x, vScrollBarRect[1], waitTime=1) x, y = thumb.MoveCursorToMyCenter() automation.DragDrop(x, y, x, vScrollBarRect[3]) mmcWindow.Close()