標籤:
AutoIt目前最新是v3版本,這是一個使用類似BASIC指令碼語言的免費軟體,它設計用於Windows GUI(圖形化使用者介面)中進行自動化操作。它利用類比鍵盤按鍵,滑鼠移動和視窗/控制項的組合來實現自動化任務。 從網站上下載AutoIt並安裝,安裝完成在菜單中會看到圖4.13的目錄: 圖4.13 AutoIt菜單 AutoIt Windows Info 用於協助我們識Windows控制項資訊。 Compile Script to.exe 用於將AutoIt產生 exe 執行檔案。 Run Script 用於執行AutoIt指令碼。 SciTE Script Editor 用於編寫AutoIt指令碼。
| <html><head><meta http-equiv="content-type" content="text/html;charset=utf-8" /><title>upload_file</title><link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" /></head><body><div class="row-fluid"><div class="span6 well"><h3>upload_file</h3><input type="file" name="file" /></div></div></body><script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script></html> |
將上面的html代碼儲存為uplad.html檔案,通過瀏覽器開啟,效果如下: 下面以操作upload.html上傳彈出的視窗為例講解AutoIt實現上傳過程。 1、首先開啟AutoIt Windows Info 工具,滑鼠點擊Finder Tool,滑鼠將變成一個小風扇形狀的表徵圖,按住滑鼠左鍵拖動到需要識別的控制項上。圖4.14 AutoIt Windows Info識別“檔案名稱”輸入框控制項 圖4.15 AutoIt Windows Info識別“開啟”按鈕控制項 4.14、4.15,通過AutoIt Windows Info 獲得以下資訊。 視窗的title為“選擇要載入的檔案”,標題的Class為“#32770”。 檔案名稱輸入框的class 為“Edit”,Instance為“1” ,所以ClassnameNN為“Edit1”。 開啟按鈕的class 為“Button”,Instance為“1” ,所以ClassnameNN為“Button1”。 2、根據AutoIt Windows Info 所識別到的控制項資訊開啟SciTE Script Editor編輯器,編寫指令碼。 ;ControlFocus("title","text",controlID) Edit1=Edit instance 1 ControlFocus("選擇要載入的檔案", "","Edit1") ; Wait 10 seconds for the Upload window to appear WinWait("[CLASS:#32770]","",10) ; Set the File name text on the Edit field ControlSetText("選擇要載入的檔案", "", "Edit1", "D:\\upload_file.txt") Sleep(2000) ; Click on the Open button ControlClick("選擇要載入的檔案", "","Button1"); ControlFocus()方法用於識別Window視窗。WinWait()設定10秒鐘用於等待視窗的顯示,其用法與WebDriver 所提供的implicitly_wait()類似。ControlSetText()用於向“檔案名稱”輸入框內輸入本地檔案的路徑。這裡的Sleep()方法與Python中time模組提供的Sleep()方法用法一樣,不過它是以毫秒為單位,Sleep(2000)表示固定休眠2000毫秒。ControlClick()用於點擊上傳視窗中的“開啟”按鈕。 AutoIt的指令碼已經寫好了,可以通過功能表列“Tools”-->“Go” (或按鍵盤F5)來運行一個指令碼吧!注意在運行時上傳視窗當前處於開啟狀態。3、指令碼運行正常,將其儲存為upfile.au3,這裡儲存的指令碼可以通過Run Script 工具將其開啟運行,但我們的目的是希望這個指令碼被Python程式調用,那麼就需要將其產生exe程式。開啟Compile Script to.exe工具,將其產生為exe可執行檔。4.16, 圖4.16 Compile Script to.exe產生exe程式 點擊“Browse”選擇upfile.au3檔案,點擊“Convert”按鈕將其產生為upfile.exe程式。 4、下面就是通過自動化測試指令碼調用upfile.exe程式實現上傳了。
| #coding=utf-8from selenium import webdriverimport osdriver = webdriver.Firefox()#開啟上傳功能頁面file_path = ‘file:///‘ + os.path.abspath(‘upfile.html‘)driver.get(file_path)#點擊開啟上傳視窗driver.find_element_by_name("file").click()#調用upfile.exe上傳程式os.system("D:\\upfile.exe")driver.quit() |
通過Python 的os模組的system()方法可以調用exe程式並執行。 瞭解了上傳的實現過程,那麼下載也是一樣的。 http://www.51testing.com/html/19/n-1265119.html
轉:Selenium藉助AutoIt識別上傳(下載)詳解