Python使用uiautomation實現Windows平台自動化


           https://github.com/yinkaisheng/Python-Automation-for-Windows

           

  • 1.uiautomation的安裝

pip install uiautomation

  • 2.uiautomation的使用

  在cmd中運行automation.py -t 3    #3秒后遍歷最上層窗口的控件   -f,  抓取焦點處控件  -n, 顯示控件的完整name   -c, 遍歷光標下的控件  -d,遍歷的層級

  import uiautomation as auto

  window=auto.WindowControl(ClassName="CabinetWClass",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等方式操作

#更改Windows控制面板中的選項

import uiautomation as auto
import subprocess
import time
import os
import locale
#獲取系統語言
if (locale.getdefaultlocale()[0] == "zh_CN"):
    Previous_Locations = "上一個位置"
    Troubleshooting = r"控制面板\所有控制面板項\疑難解答"
    Change_settings = "更改設置"
    Close = "關閉"
else:
    Previous_Locations = "Previous Locations"
    Troubleshooting = r"Control Panel\All Control Panel Items\Troubleshooting"
    Change_settings = "Change settings"
    Close = "Close"

try:
    subprocess.Popen('Control.exe')
    window = auto.WindowControl(ClassName="CabinetWClass", searchDepth=1)
    window.Maximize()
    # 窗口最大化,避免因分辨率的不同導致元素無法選中
    # auto.SendKeys("{WIN}{UP}") #窗口最大化快捷鍵
    button = auto.ButtonControl(searchDepth=7, Name=Previous_Locations)
    button.Click()
    auto.SendKeys(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')
    # RadioButton 是否選中,Bool值 選中為True 未選中為False
    radioValue = radio.GetSelectionItemPattern().IsSelected
    # checkbox ToggleState 
    checkbox = auto.CheckBoxControl(searchDepth=9, ClassName='CCCheckBox', AutomationId='skipCheckbox')
    SettingOK = auto.ButtonControl(searchDepth=9, ClassName='CCPushButton', AutomationId="settingOK")
    CloseWindow = auto.ButtonControl(searchDepth=3, Name=Close)
    flag = False
    # 檢查設置狀態 ,如果有修改 ,flag = True, 點擊OK 保存 ,如不需修改 直接關閉窗口
    if (radioValue == False):
        radio.Click()
        flag = True
    if (checkbox.GetTogglePattern().ToggleState == 1):
        checkbox.Click()
        flag = True
    if (flag == True):
        SettingOK.Click()
        
    CloseWindow.Click()    
        
except:
    print("Fail")
    os.system("pause")
else:
    print("Pass")
    os.system("pause")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM