遍历所有窗口名及模糊查找窗口名

有的窗口名经常变化又无法事先获得窗口名,但只知道部分名字,导致有的组件的标题属性无法填写,有的组件又无法使用模糊窗口名,比如 10.0、10.1.1 等版本的 firefox 等组件就无法使用模糊窗口名(目前发现的,其它的未验证),那么可以在问题修正前自行解决,参考下面的代码,原则上支持 RPA8-RPA10.1.1 之间的版本,其它版本未测试。

import re
import ubpa.base_native_ait as nit

#模糊查找窗口名
def get_win_name(fuzzy_name='百度搜索'):
    get_name=None
    try:
        #遍历当前所有窗口
        win_list = get_win_list() 
        for i in win_list:
            if fuzzy_name in i: 
                get_name = i
                break
    except Exception as e:
        print(e)
    finally:
        return get_name


#遍历窗口标题
def get_win_list():
    win_list=None
    try:
        msg = "#include <MsgBoxConstants.au3>" \
          + '\n' + "Local $str = ''" \
          + '\n' + "Local $aList = WinList()" \
          + '\n' + "For $i = 1 To $aList[0][0]" \
          + '\n' + "  If $aList[$i][0] <> '' And BitAND(WinGetState($aList[$i][1]), 2) Then" \
          + '\n' + "      $str = $str &','& $aList[$i][0]" \
          + '\n' + "  EndIf" \
          + '\n' + "Next" \
          + '\n' + 'ConsoleWrite($str)'
        tmp_au3_file_path = nit.gen_au3_file(msg)
        status, error_string, stdout_string = nit.run_autoit(tmp_au3_file_path)
        nit.cleanup(tmp_au3_file_path)
        p_list = str(u' '.join(line for line in stdout_string.decode('gbk').splitlines()).strip())
        win_list = re.split(',',p_list)
        return win_list
    except Exception as e:
        print(e)
    finally:
        return win_list