• 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 PanelAll Control Panel ItemsTroubleshooting"
        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")
  • 相关阅读:
    什么是线程池?
    线程包括哪些状态?状态之间是如何变化的?
    C语言中的文本流与二进制流的区别
    Windows+IIS结合LVS+Keepalived是实现Linux负载均衡软件
    Linux中的lo回环接口详细介绍
    Keepalived原理与实战精讲
    分布式文件系统MFS(moosefs)实现存储共享(第二版)
    C语言缓冲区(缓存)详解
    Android如何防止apk程序被反编译
    Android APK反编译详解(附图)
  • 原文地址:https://www.cnblogs.com/jyang/p/11679828.html
Copyright © 2020-2023  润新知