一、自己封裝的一些使用到的autoit庫
import autoit class MouseControl(object): ''' AutoIt鼠標相關操作 ''' def __init__(self): ''' Constructor ''' def click(self, title, text, x, y, button="main", clicks=1): ''' :description 執行鼠標點擊操作 ''' pos = autoit.win_get_pos(title, text=text) autoit.mouse_click(button, x + pos[0], y + pos[1], clicks=clicks) def move(self, title, text, x, y): ''' :description 移動鼠標指針 ''' pos = autoit.win_get_pos(title, text=text) autoit.mouse_move(x + pos[0], y + pos[1]) def drag(self, title, text, x1, y1, x2, y2): ''' :description 執行鼠標拖拽操作 ''' pos = autoit.win_get_pos(title, text=text) autoit.mouse_click_drag(x1 + pos[0], y1 + pos[1], x2 + pos[0], y2 + pos[1]) def wheel(self, direction="up"): ''' :description 產生向上或向下滾動鼠標滾輪事件.僅支持NT/2000/XP及更高. ''' autoit.mouse_wheel(direction) mouseControl.py
import autoit class ProcessControl(object): ''' AutoIt進程相關操作 ''' def __init__(self, processName): ''' Constructor ''' self.processName = processName def close(self): ''' :description 終止某個進程 :return 1:成功; 0:失敗. ''' return autoit.process_close(self.processName) def exists(self): ''' :description 檢查指定進程是否存在 :return PID:成功; 0:進程不存在. ''' return autoit.process_exists(self.processName) processControl
import autoit class WinControl(object): ''' AutoIt窗口相關操作 ''' def __init__(self, title, text=''): ''' Constructor ''' self.title = title self.text = text def activate(self): ''' :description 激活指定的窗口(設置焦點到該窗口,使其成為活動窗口). :return PID:窗口存在; 0:窗口不存在. ''' return autoit.win_activate(self.title, text=self.text) def close(self): ''' :description 關閉指定窗口. :return 1:成功; 0:窗口不存在. ''' return autoit.win_close(self.title, text=self.text) def exists(self): ''' :description 檢查指定的窗口是否存在. :return 1:窗口存在; 0:窗口不存在. ''' return autoit.win_exists(self.title, text=self.text) def getPos(self): ''' :description 獲取指定窗口的坐標位置和大小等屬性. :return Returns left, top, right, bottom (x1,y1,x2,y2) ''' return autoit.win_get_pos(self.title, text=self.text) def getProcess(self): ''' :description 獲取指定窗口關聯的進程ID(PID). :return PID:成功, -1:失敗. ''' return autoit.win_get_process(self.title, text=self.text) def getText(self, buf_size=256): ''' :description 獲取指定窗口中的文本. :return 指定窗口里包含的文本:成功; 0:失敗(沒有匹配的窗口). ''' return autoit.win_get_text(self.title, buf_size, text=self.text) def kill(self): ''' :description 強行關閉指定窗口. :return 1:無論成功失敗. ''' return autoit.win_kill(self.title, text=self.text) def move(self, x, y, width, height): ''' :description 移動指定的窗口或調整窗口的大小. :return PID:成功, 0:失敗(窗口不存在). ''' return autoit.win_move(self.title, x, y, width, height, text=self.text) def setState(self, flag): ''' :description 顯示,隱藏,最小化,最大化或還原一個窗口. :param flag: The "show" flag of the executed program: = 顯示 = 最小化/隱藏 = 最大化 = 還原 :return 1:成功, 0:失敗(窗口不存在). ''' return autoit.win_set_state(self.title, flag, text=self.text) def wait(self, timeout=5): ''' :description 暫停腳本的執行直至指定窗口存在(出現)為止. timeout 單位為秒. :return PID:成功, 0:失敗(超時). ''' return autoit.win_wait(self.title, timeout, text=self.text) def waitActive(self, timeout=5): ''' :description 暫停腳本的執行直至指定窗口被激活(成為活動狀態)為止. timeout 單位為秒. :return PID:成功, 0:失敗(超時). ''' return autoit.win_wait_active(self.title, timeout, text=self.text) def waitClose(self, timeout=5): ''' :description 暫停腳本的執行直至所指定窗口不再存在為止. timeout 單位為秒. :return 1:成功, 0:失敗(超時). ''' return autoit.win_wait_close(self.title, timeout, text=self.text) def waitNotActive(self, timeout=5): ''' :description 暫停腳本的執行直至指定窗口不是激活狀態為止. timeout 單位為秒. :return 1:成功, 0:失敗(超時). ''' return autoit.win_wait_not_active(self.title, timeout, text=self.text) def controlClick(self, control, button="main", clicks=1): ''' :description 向指定控件發送鼠標點擊命令. ''' autoit.control_click(self.title, control, text=self.text, button=button, clicks=clicks) def controlCommand(self, control, command, extra="", buf_size=256): ''' :description 向指定控件發送命令. :param command, extra: :return "IsVisible", "" 1:可見; 0:不可見 "IsEnabled", "" 1:可用; 0:禁用 "ShowDropDown", "" 彈出/下拉 組合框(ComboBox)的列表. "HideDropDown", "" 收回/隱藏 組合框(ComboBox)的列表. "AddString", "string" 在 ListBox 或 ComboBox 的編輯框后面附加指定字符串. "DelString", 出現次序 刪除在 ListBox 或 ComboBox 的編輯框中指定的字符串(從0開始). "FindString", "string" 返回在 ListBox 或 ComboBox 的編輯框中與指定字符串匹配項目的出現次序(從0開始). "SetCurrentSelection", 出現次序 通過指定出現次序(從0開始)把 ListBox 或 ComboBox 的當前選擇項設為指定的項目. "SelectString","string" 通過指定字符串把 ListBox 或 ComboBox 的當前選擇項設為匹配字符串的項目. "IsChecked", "" 若目標按鈕(復選框/單選框)被選中則返回值為1,否則為0. "Check", "" 使目標按鈕(復選框/單選框)變為選中狀態. "UnCheck", "" 使目標按鈕(復選框/單選框)變為非選中狀態. "GetCurrentLine", "" 返回在目標編輯框中插入符(caret,光標)的所在行號. "GetCurrentCol", "" 返回在目標編輯框中插入符(caret,光標)的所在列號. "GetCurrentSelection", "" 返回 ListBox 或 ComboBox 控件當前選中的項目名. "GetLineCount", "" 返回目標編輯框中的總行數. "GetLine", 行號 返回目標編輯框中指定行的文本內容. "GetSelected", "" 返回目標編輯框中的(用戶用鼠標或其它方式)選定的文本. "EditPaste", 'string' 在目標編輯框中插入符(caret)所在位置后插入指定字符串. "CurrentTab", "" 返回在 SysTabControl32 控件中當前顯示的標簽編號(從1開始). "TabRight", "" 使 SysTabControl32 控件切換到(右邊的)下一個標簽. "TabLeft", "" 使 SysTabControl32 控件切換到(左邊的)下一個標簽. "SendCommandID", 命令 ID 模擬 WM_COMMAND 消息. 通常用於 ToolbarWindow32 控件 - 使用Au3Info的工具欄標簽得到命令ID. ''' return autoit.control_command(self.title, control, command, buf_size, text=self.text, extra=extra) def controlListView(self, control, command, extra1, extra2="", buf_size=256): ''' :description 向指定的 ListView32 控件發送命令. :param command, extra1, extra2: :return "DeSelect", 從[, 到] 取消選定從"從"開始直到"到"的一個或多個項目. "FindItem", "搜索字符串" [, 子項目] 返回與給定字符串匹配的項目的位置.若未找到指定字符串則返回值為 -1. "GetItemCount" 返回列表中項目的數量. "GetSelected" [, 選項] 返回當前選中項目的位置.若 選項=0(默認)則只返回選中的第一個項目;若 選項=1 則返回由豎線"|"作為分隔符的所有選中項目,例如:"0|3|4|10".若沒有選中任何項目則返回一個空字符串"". "GetSelectedCount" 返回選中項目的數量. "GetSubItemCount" 返回子項目的數量. "GetText", 項目, 子項目 返回指定項目/子項目的文本. "IsSelected", 項目 若指定項目被選中則返回值為1,否則返回值為0. "Select", 從[, 到] 選中一個或多個項目(請參考第一個命令). "SelectAll" 選中所有項目. "SelectClear" 取消所有項目的選中狀態. "SelectInvert" 切換當前的選中狀態. "ViewChange", "視圖" 切換當前的視圖.可用的視圖包括"list"(列表),"details"(詳細信息),"smallicons"(小圖標),"largeicons"(大圖標). ''' return autoit.control_list_view(self.title, control, command, buf_size, text=self.text, extra1=extra1, extra2=extra2) def controlDisable(self, control): ''' :description 禁用或使某控件變成灰色不可用狀態. :return 1:成功; 0:失敗. ''' return autoit.control_disable(self.title, control, text=self.text) def controlEnable(self, control): ''' :description 使灰色按鈕/控件變為"可用"狀態. :return 1:成功; 0:失敗. ''' return autoit.control_enable(self.title, control, text=self.text) def controlFocus(self, control): ''' :description 設置輸入焦點到指定窗口的某個控件上. :return 1:成功; 0:失敗. ''' return autoit.control_focus(self.title, control, text=self.text) def controlGetText(self, control): ''' :description 獲取指定控件上的文本. :return 文本內容:成功; 空:失敗. ''' return autoit.control_get_text(self.title, control, text=self.text) def controlSend(self, control, send_text, mode=0 ): ''' :description 向指定的控件發送字符串. :param mode: 0:按特殊字符含義發送(默認); 1:原樣發送. :return 1:成功; 0:失敗(窗口/控件未找到). ''' return autoit.control_send(self.title, control, send_text, mode, text=self.text) def controlSetText(self, control, control_text): ''' :description 修改指定控件的文本. :return 1:成功; 0:失敗(窗口/控件未找到). ''' return autoit.control_set_text(self.title, control, control_text, text=self.text) def controlTreeView(self, control, command, extra, buf_size=256): ''' :description 發送一個命令到 TreeView32 控件.(子節點不好用*) :param command, extra :return "Check", "項目" 選中一個項目 (如果項目支持選中,這里指項目帶有選擇框). "Collapse", "項目" 折疊一個項目節點,使它隱藏它的子項目. "Exists", "項目" *都返回1* 如果項目存在返回 1,否則返回 0. "Expand", "項目" 展開一個項目節點,使它顯示它的子項目. "GetItemCount", "項目" 返回所選項目的子項目數量. "GetSelected" [, 使用索引] 返回當前所選項目的文本參考信息(如果使用索引設置為1將會返回所選項目索引位置). "GetText", "項目" 返回項目文本. "IsChecked" 返回項目選中狀態. 1:被選中, 0:未被選中, -1:沒要選擇框. "Select", "項目" 選擇一個項目. "Uncheck", "項目" 取消項目選中狀態 (如果項目支持選中,這里指項目帶有選擇框). ''' return autoit.control_tree_view(self.title, control, command, text=self.text, buf_size=buf_size, extra=extra) def statusbarGetText(self, part=1, buf_size=256): ''' :description 獲取標准狀態欄控件的文本. :return 文本內容:成功; 空字符串:失敗(無法讀取文本). ''' return autoit.statusbar_get_text(self.title, self.text, part, buf_size) winControl
有些控件操作沒有原生的autoit支持的好,比如對樹控件的操作,經常會有問題,如果遇到出現問題只能再換autoit寫au3文件直接運行了......
二、遇到的坑
1. 在等待窗口出現時,比較了try和while方法,覺得還是while方法比較方面和好用,尤其是你將等待出現的窗口可能有好幾種不同的窗口時,用while+if就很方便。
def isclientupdate(): '''判斷包是否需要升級''' w_clientupdate = winControl.WinControl(title_update) w_clientupdate_choice = winControl.WinControl(title_choice) flag = True count = 0 while flag: if w_clientupdate.exists(): clientupdate() flag = False elif w_clientupdate_choice.exists(): w_clientupdate_choice.controlClick('Button2') clientupdate() flag = False else: time.sleep(1) count += 1 if count == 5: clientlogin() flag = False