- 1.uiautomation的安装
pip install uiautomation
- 2.uiautomation的使用
在cmd中运行automation.py -t 3 #3秒后抓取topmost 程序的属性 -f 抓取鼠标指针处元素属性
window=auto.WindowControl(ClassName="Window",searchDepth=1)
window.SwitchToThisWindow() # 选择窗口
1.抓取不到Button属性,使用键盘操作
import os import time import subprocess from pykeyboard import PyKeyboard #需先安装PyUserInput model import uiautomation as auto subprocess.Popen("3dmark-setup.exe") k = PyKeyboard() # os.system("start 3dmark-setup.exe") while True: try: welcome=auto.TabItemControl(Name="WELCOME",searchDepth=3) #此应用无法抓取具体Button,通过键盘输入 Tab Space Enter等来操作,需要判断此时处于哪一页面 if(welcome.GetSelectionItemPattern().IsSelected): #此处根据IsSelected判断是否在此页面,Bool值 print("Find WELCOME") time.sleep(2) k.tap_key(k.enter_key) break except: print("Can't Find WELCOME") while True: try: welcome=auto.TabItemControl(Name="EULA",searchDepth=3) if(welcome.GetSelectionItemPattern().IsSelected): print("Find EULA") time.sleep(2) k.tap_key(k.tab_key,n=3) k.tap_key(k.space_key) k.tap_key(k.enter_key) break except: print("Can't Find EULA") while True: try: welcome=auto.TabItemControl(Name="OPTIONS",searchDepth=3) if(welcome.GetSelectionItemPattern().IsSelected): print("Find OPTIONS") time.sleep(2) k.tap_key(k.enter_key) break except: print("Can't Find OPTIONS") while True: try: welcome=auto.TabItemControl(Name="INSTALL",searchDepth=3) if(welcome.GetSelectionItemPattern().IsSelected): print("Installing...") time.sleep(2) k.tap_key(k.enter_key) break except: print("Can't Find INSTALL") while True: try: welcome=auto.TabItemControl(Name="COMPLETE",searchDepth=3) if(welcome.GetSelectionItemPattern().IsSelected): print("Find COMPLETE") time.sleep(2) k.tap_key(k.enter_key) break except: print("Can't Find COMPLETE")
2.可以抓取到Button等元素的属性,使用Click等方式操作
import uiautomation as auto, subprocess,time,os try: subprocess.Popen('Control.exe') time.sleep(2) auto.SendKeys("{WIN}{UP}") #窗口最大化,避免因分辨率的不同导致元素无法选中 time.sleep(1) button = auto.ButtonControl(searchDepth=7, Name='Previous Locations') button.Click() auto.SendKeys('Control Panel\\All Control Panel Items\\Troubleshooting') auto.SendKeys('{Enter}') link = auto.HyperlinkControl(searchDepth=7, ClassName='ControlPanelLink', Name='Change settings') link.Click() radio = auto.RadioButtonControl(searchDepth=9, ClassName='CCRadioButton', AutomationId='autoOff') radioValue = radio.GetSelectionItemPattern().IsSelected #Bool值 选中为True 未选中为False checkbox = auto.CheckBoxControl(searchDepth=9, ClassName='CCCheckBox', AutomationId='skipCheckbox') if(radioValue==True and checkbox.GetTogglePattern().ToggleState==0): #checkbox ToggleState CloseWindow=auto.ButtonControl(searchDepth = 3,Name="Close") CloseWindow.Click() elif(radioValue==True and checkbox.GetTogglePattern().ToggleState==1): checkbox.Click() SettingOK=auto.ButtonControl(searchDepth = 9, ClassName = 'CCPushButton',AutomationId= "settingOK") SettingOK.Click() CloseWindow=auto.ButtonControl(searchDepth = 3,Name="Close") CloseWindow.Click() elif(radioValue==False and checkbox.GetTogglePattern().ToggleState==0): radio.Click() SettingOK=auto.ButtonControl(searchDepth = 9, ClassName = 'CCPushButton',AutomationId= "settingOK") SettingOK.Click() CloseWindow=auto.ButtonControl(searchDepth = 3,Name="Close") CloseWindow.Click() else: radio.Click() checkbox.Click() SettingOK=auto.ButtonControl(searchDepth = 9, ClassName = 'CCPushButton',AutomationId= "settingOK") SettingOK.Click() CloseWindow=auto.ButtonControl(searchDepth = 3,Name="Close") CloseWindow.Click() except: print("fail") os.system("pause") else: print("pass") os.system("pause")