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
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)
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)
auto.Logger.WriteLine(' ' * (depth - 1) * 4 + item.Name, auto.ConsoleColor.Green)
def main():
consoleWindow = automation.GetConsoleWindow()
print(consoleWindow)
print('\nconsole window will be hidden in 3 seconds')
time.sleep(3)
consoleWindow.Hide()
time.sleep(2)
print('\nconsole window shows again')
consoleWindow.Show()
raise
newTab = tab.TabItemControl(SubName = 'yinkaisheng/Python-UIAutomation-for-Windows')
newTab.Click()
starButton = firefoxWindow.ButtonControl(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)
else:
unstarButton = firefoxWindow.ButtonControl(Name = 'Unstar this repository')
if unstarButton.Exists(0, 0):
automation.GetConsoleWindow().SetActive()
automation.Logger.WriteLine('Thank you. You have starred.', automation.ConsoleColor.Yellow)
def main():
main = threading.currentThread()
auto.Logger.WriteLine('This is running in main thread. {} {}'.format(main.ident, main.name), auto.ConsoleColor.Cyan)
time.sleep(2)
auto.GetConsoleWindow().CaptureToImage('console_mainthread.png')
root = auto.GetRootControl()
auto.EnumAndLogControl(root, 1)
th = threading.Thread(target=threadFunc, args=(root, ))
th.start()
th.join()
auto.Logger.WriteLine('\nMain thread exits. {} {}'.format(main.ident, main.name), auto.ConsoleColor.Cyan)
def DemoCN():
"""for Chinese language"""
thisWindow = auto.GetConsoleWindow()
auto.Logger.ColorfullyWrite('我将运行cmd并设置它的屏幕缓冲区使cmd一行能容纳很多字符\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()
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--main', action='store_true', help='exec main')
parser.add_argument('--hide', action='store_true', help='hide window')
parser.add_argument('--show', action='store_true', help='show window')
args = parser.parse_args()
if args.main:
if args.hide:
hide()
elif args.show:
show()
else:
subprocess.Popen('notepad')
auto.GetConsoleWindow().SetActive()
auto.Logger.ColorfullyWriteLine('Press Ctr+1 to hide\nPress Ctr+2 to show\n')
auto.RunByHotKey({(auto.ModifierKey.Control, auto.Keys.VK_1): lambda e: HotKeyFunc(e, ['--main', '--hide']),
(auto.ModifierKey.Control, auto.Keys.VK_2): lambda e: HotKeyFunc(e, ['--main', '--show']),
},
(auto.ModifierKey.Control, auto.Keys.VK_9))