注意:適用於62位windows7系統,32位只能作參考
1.下載sikuli的jar包
下載地址:https://launchpad.net/sikuli
我使用的版本為:sikulixsetup-1.1.2.jar
2.安裝sikuli環境
在D盤新建一個目錄sikuli,將sikulixsetup-1.1.2.jar復制到該目錄中,雙擊sikulixsetup-1.1.2.jar文件,
運行該jar包如下圖所示,點擊 是
勾選 Pack2 即安裝 sikulix 的 api 包,點擊 Setup Now

點擊 確認窗口的 是
安裝完成后,sikuli 目錄中會自動生成一個 sikulixapi.jar 包,這是以后我們 Python 腳本要使
用的包,后續需要引用此包

3.下載 JPype1(調用 Java 程序的 Python 第三方庫)
官方下載地址:https://pypi.org/project/JPype1/
4.安裝 JPype1 第三方庫
離線安裝方式(推薦):
將 JPype1-0.6.3-py3.5-win-amd64.egg.rar 壓縮包下載到本地
下載地址:鏈接:
https://pan.baidu.com/s/1Jy1hfcLITZsjvnZDtMsRfw 提取碼:1p3g
1)先將 JPype1-0.6.3-py3.5-win-amd64.egg.rar 包解壓到 Python35\Lib\site-packages 目錄中(即
Python 安裝目錄)
2)再用記事本修改 easy-install.pth 文件,末尾換行加入:./jpype1-0.6.3-py3.5-win-amd64.egg
3)重新進入 Python 命令行模式,輸入 import jpype 沒報錯即表示成功
在線安裝方式:

5.JRE 環境配置
由於利用 Python 加載使用 Jar 包,所以需要開啟 JVM(Java 虛擬機)環境,需要配置 Java
開發環境,環境配置請參見另外一份名為 Python+Eclipse+Selenium 環境搭建的說明文章
確認如:C:\Program Files\Java\jdk1.8.0_151\jre\bin\server 目錄下有 jvm.dll 文件即可
6.項目中引用
在需要加載使用 JVM 的代碼中,加入以下代碼即可:
import jpype
#引用jvm
jvm_path=r"C:\Program Files\Java\jdk1.8.0_151\jre\bin\server\jvm.dll"
#開啟jvm
jpype.startJVM(jvm_path)
#測試
jpype.java.lang.System.out.println("Hello world!")
#關閉虛擬機
jpype.shutdownJVM()
常見問題:
1.命令行中導包提示找不到模塊,但 eclipse 中不會報錯
原因:Windows 系統環境未知原因導致
解決辦法:不影響使用可暫不管
2.命令行中導包沒問題,但 eclipse 中啟動 JVM 虛擬機時, python 會提示停止運行
原因:Windows 系統環境未知原因導致
解決辦法:重裝系統…..
3.eclipse 中編寫好測試腳本后,可運行,但導包時提示 Unresolved import:
jpype,如下圖:

原因:安裝 jpype 第三方庫后,未更新 eclipse 對應的解釋器
解決辦法:重新配置下解釋器,參考下面窗口的步驟,刪除了重新配置。如果還是提示
Unresolved,則關掉腳本重新打開就好了
https://www.cnblogs.com/jaww/p/9544992.html
python+autoit用法
一、自己封裝的一些使用到的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