標籤:實現 編寫 utf-8 滑鼠 狀態 instance 上傳 tools sci
AutoIt安裝
1、首先開啟AutoIt Windows Info 工具,滑鼠點擊Finder Tool,滑鼠將變成一個小風扇形狀的表徵圖,按住滑鼠左鍵拖動到需要識別的控制項上。
通過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可執行檔。
點擊“Browse”選擇upfile.au3檔案,點擊“Convert”按鈕將其產生為upfile.exe程式。
4、下面就是通過自動化測試指令碼調用upfile.exe程式實現上傳了。
#coding=utf-8
from selenium import webdriver
import os
driver = 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()
web自動化9-selenium.Windows上傳檔案