selenium藉助AutoIt識別上傳(下載)詳解

來源:互聯網
上載者:User

標籤:asi   運行   tool   表徵圖   uid   指令碼   set   使用者   inf   

轉自:http://www.cnblogs.com/fnng/p/4188162.html

AutoIt目前最新是v3版本,這是一個使用類似BASIC指令碼語言的免費軟體,它設計用於Windows GUI(圖形化使用者介面)中進行自動化操作。它利用類比鍵盤按鍵,滑鼠移動和視窗/控制項的組合來實現自動化任務。 

     官方網站:https://www.autoitscript.com/site/

    從網站上下載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 1ControlFocus("選擇要載入的檔案", "","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程式並執行。

瞭解了上傳的實現過程,那麼下載也是一樣的。

 

selenium藉助AutoIt識別上傳(下載)詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.