Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def walk():
wiresharkWindow = None
for win in auto.GetRootControl().GetChildren():
if win.ClassName == 'MainWindow' and win.AutomationId == 'MainWindow':
if win.ToolBarControl(AutomationId='MainWindow.displayFilterToolBar').Exists(0, 0):
wiresharkWindow = win
break
if not wiresharkWindow:
auto.Logger.WriteLine('Can not find Wireshark', auto.ConsoleColor.Yellow)
return
console = auto.GetConsoleWindow()
if console:
sx, sy = auto.GetScreenSize()
tp = console.GetTransformPattern()
tp.Resize(sx, sy // 4)
tp.Move(0, sy - sy // 4 - 50)
console.SetTopmost()
wiresharkWindow.SetActive(waitTime=0.1)
wiresharkWindow.Maximize()
auto.Logger.ColorfullyWriteLine('Press F1 to stop', auto.ConsoleColor.Yellow)
tree = wiresharkWindow.TreeControl(searchDepth=4, ClassName='PacketList', Name='Packet list')
rect = tree.BoundingRectangle
tree.Click(y=50, waitTime=0.1)
auto.SendKeys('{Home}', waitTime=0.1)
columnCount = 0
treeItemCount = 0
for item, depth in auto.WalkControl(tree):
if isinstance(item, auto.HeaderControl):
def testNotepadEN():
consoleWindow = auto.GetConsoleWindow()
consoleWindow.SetActive()
auto.Logger.ColorfullyWriteLine('\nI will open Notepad and automate 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()
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 Notepad and type Hello!!!\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 "automation.py -h" to display the help\n\n')
time.sleep(3)
cmdWindow.SendKeys('"{}" -h'.format(scriptPath) + '{Enter}', 0.05)
time.sleep(3)
thisWindow.SetActive()
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('我将运行记事本并输入Hello!!!\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('运行"automation.py -h"显示帮助\n\n')
time.sleep(3)
cmdWindow.SendKeys('"{}" -h'.format(scriptPath) + '{Enter}', 0.05)
time.sleep(3)
thisWindow.SetActive()
def main():
sw, sh = auto.GetScreenSize()
cmdWindow = auto.GetConsoleWindow()
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)
consoleWindow = auto.GetConsoleWindow()
consoleWindow.SetActive()
auto.Logger.ColorfullyWriteLine('\nI will open Notepad and automate it. Please wait for a while.')
time.sleep(2)
auto.ShowDesktop()
#打开notepad
subprocess.Popen('notepad')
#查找notepad, 如果name有中文,python2中要使用Unicode
window = auto.WindowControl(searchDepth = 1, ClassName = 'Notepad', RegexName = '.* - 记事本')
#可以判断window是否存在,如果不判断,找不到window的话会抛出异常
#if window.Exists(maxSearchSeconds = 3):
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
edit = auto.EditControl(searchFromControl = window) #or edit = window.EditControl()
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')
window.CaptureToImage('Notepad.png')