Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
def testNotepadCN():
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')
def HotKeyFunc(stopEvent: 'Event', argv: list):
args = [sys.executable, __file__] + argv
cmd = ' '.join('"{}"'.format(arg) for arg in args)
auto.Logger.WriteLine('call {}'.format(cmd))
p = subprocess.Popen(cmd)
while True:
if None != p.poll():
break
if stopEvent.is_set():
childProcesses = [pro for pro in psutil.process_iter() if pro.ppid == p.pid or pro.pid == p.pid]
for pro in childProcesses:
auto.Logger.WriteLine('kill process: {}, {}'.format(pro.pid, pro.cmdline()), auto.ConsoleColor.Yellow)
p.kill()
break
stopEvent.wait(0.05)
auto.Logger.WriteLine('HotKeyFunc exit')
if find:
break
else:
automation.SendKeys('{Esc}')
time.sleep(0.5)
if not find:
automation.Logger.Log('this pdf not support editing')
exit(0)
menuItem.Click(simulateMove = False, waitTime = 0.2)
edit = automation.EditControl(searchFromControl= tree, searchDepth= 1)
edit.SetValue(newName, waitTime = 0.05)
automation.SendKeys('{Enter}', waitTime = 0.05)
automation.Logger.Write('rename: ')
automation.Logger.WriteLine(name, automation.ConsoleColor.Green)
automation.Logger.Write(' @@to : ' if alert else ' to: ')
automation.Logger.WriteLine(newName, automation.ConsoleColor.Yellow if alert else automation.ConsoleColor.Green)
if depth + 1 > TreeDepth:
return
if automation.ExpandCollapseState.Collapsed == treeItem.CurrentExpandCollapseState():
treeItem.Expand(0.1)
childItems = treeItem.GetChildren()
if childItems:
for child in childItems:
RenameTreeItem(tree, child, bookMark.children, depth + 1, removeChapter)
def HotKeyFunc(stopEvent: 'Event', argv: list):
args = [sys.executable, __file__] + argv
cmd = ' '.join('"{}"'.format(arg) for arg in args)
auto.Logger.WriteLine('call {}'.format(cmd))
p = subprocess.Popen(cmd)
while True:
if p.poll() is not None:
break
if stopEvent.is_set():
childProcesses = [pro for pro in psutil.process_iter() if pro.ppid == p.pid or pro.pid == p.pid]
for pro in childProcesses:
auto.Logger.WriteLine('kill process: {}, {}'.format(pro.pid, pro.cmdline()), auto.ConsoleColor.Yellow)
p.kill()
break
stopEvent.wait(0.05)
auto.Logger.WriteLine('HotKeyFunc exit')
control = auto.GetRootControl()
if focus:
control = auto.GetFocusedControl()
if cursor:
control = auto.ControlFromCursor()
if depth < 0:
while depth < 0 and control:
control = control.GetParentControl()
depth += 1
depth = 0xFFFFFFFF
if ancestor:
control = auto.ControlFromCursor()
if control:
auto.EnumAndLogControlAncestors(control, showAllName)
else:
auto.Logger.Write('IUIAutomation returns null element under cursor\n', auto.ConsoleColor.Yellow)
else:
indent = 0
if not control:
control = auto.GetFocusedControl()
controlList = []
while control:
controlList.insert(0, control)
control = control.GetParentControl()
if len(controlList) == 1:
control = controlList[0]
else:
control = controlList[1]
if foreground:
indent = 1
auto.LogControl(controlList[0], 0, showAllName)
auto.EnumAndLogControl(control, depth, showAllName, startDepth=indent)
def CaptureControl(c, path, up = False):
if c.CaptureToImage(path):
auto.Logger.WriteLine('capture image: ' + path)
else:
auto.Logger.WriteLine('capture failed', auto.ConsoleColor.Yellow)
if up:
r = auto.GetRootControl()
depth = 0
name, ext = os.path.splitext(path)
while True:
c = c.GetParentControl()
if not c or auto.ControlsAreSame(c, r):
break
depth += 1
savePath = name + '_p' * depth + ext
if c.CaptureToImage(savePath):
auto.Logger.WriteLine('capture image: ' + savePath)
subprocess.Popen(path, shell = True)
def AnalyzeUI(sampleRate = 90000, payload = 96, beginNo = 0, maxPackets = 0xFFFFFFFF, showLost = False):
"""Wireshark version must >= 2.0"""
wireSharkWindow = automation.WindowControl(searchDepth= 1, ClassName = 'Qt5QWindowIcon')
if wireSharkWindow.Exists(0, 0):
wireSharkWindow.SetActive()
else:
automation.Logger.WriteLine('can not find wireshark', automation.ConsoleColor.Yellow)
return
tree = wireSharkWindow.TreeControl(searchDepth= 4, SubName = 'Packet list')
left, top, right, bottom = tree.BoundingRectangle
tree.Click(10, 30)
automation.SendKeys('{Home}{Ctrl}{Alt}4')
time.sleep(0.5)
tree.Click(10, 30)
headers = []
headerFunctionDict = {'No': int,
'Time': float,
'Source': str,
'Destination': str,
'Protocol': str,
'Length': int,
'Info': str,}
index = 0
columnCount = 0
treeItemCount = 0
for item, depth in auto.WalkControl(tree):
if isinstance(item, auto.HeaderControl):
columnCount += 1
auto.Logger.Write(item.Name + ' ')
elif isinstance(item, auto.TreeItemControl):
if treeItemCount % columnCount == 0:
auto.Logger.Write('\n')
time.sleep(0.1)
treeItemCount += 1
auto.Logger.Write(item.Name + ' ')
if item.BoundingRectangle.bottom >= rect.bottom:
auto.SendKeys('{PageDown}', waitTime=0.1)
if auto.IsKeyPressed(auto.Keys.VK_F1):
auto.Logger.WriteLine('\nF1 pressed', auto.ConsoleColor.Yellow)
break