Selenium+java上傳檔案

來源:互聯網
上載者:User

標籤:gcc   pdo   tpi   pwm   jni   lte   jms   osx   ldd   

自動化調用:

  • AutoIT指令碼編譯成可執行檔後,放在本地的某一個目錄下
  • 上傳檔案時,首先定位到【上傳】字樣文本,點擊該按鈕
  • 執行編輯後的可執行檔,實現檔案上傳

一、安裝AutoIT3,主要用到的工具

  AutoIt Windows Info   用於協助我們識Windows控制項資訊。

  Compile Script to.exe 用於將AutoIt產生 exe 執行檔案。

  Run Script            用於執行AutoIt指令碼。

  SciTE Script Editor   用於編寫AutoIt指令碼。

  1)、用AutoIt Windows Info  識別Windows控制項資訊,如按鈕

  

2、用SciTE Script Editor編寫指令碼,指令碼內容如下:

  ;first make sure the number of arguments passed into the scripts is more than 1
  If $CmdLine[0]<2 Then Exit EndIf ;if parmas num <2 ,then break
  ;$CmdLine[0] ;參數的數量
  ;$CmdLine[1] ;第一個參數 (指令碼名稱後面)
  ;$CmdLine[2] ;第二個參數
  ;都是從cmd傳入參數
  handleUpload($CmdLine[1],$CmdLine[2])

  ;定義上傳函數,有兩個參數,第一個是瀏覽器名字,第二參數是檔案路徑
   Func handleUpload($browser, $uploadfile)
       Dim $title                          ;定義一個title變數
              ;根據彈窗的title來判斷是什麼瀏覽器
              If $browser="ie" Then                          ; 代表IE瀏覽器
                    $title="選擇要載入的檔案"
              ;ElseIf $browser="chrome" Then               ; 代表Google瀏覽器
                 ;  $title="開啟"
              ElseIf    $browser="firefox" Then             ; 代表Firefox瀏覽器
                    $title="檔案上傳"
              EndIf

              if WinWait($title,"",4) Then ;等待彈出出現,最大等待時間是4秒
                     WinActivate($title)                  ;找到快顯視窗之後,啟用當前視窗
                     ControlSetText($title,"","Edit1",$uploadfile)   ;把檔案路徑放入輸入框,此Edit1是用FinderTool擷取到的
                     ControlClick($title,"","Button1")                ;點擊儲存或者開啟或者上傳按鈕,此Button1使用FinderTool擷取到的
              Else
                Return False
              EndIf
   EndFunc

  說明:title是AutoIt Window Info識別出的Title欄位,controlID是AutoIt Window Info識別出的Class和Instance的拼接

  運行指令碼,Toos-->Go,運行時,上傳視窗處於開啟狀態。

  3)、用Complie Script to .exe工具,將指令碼編譯成可執行檔,即編譯成.exe格式

  

二、執行AutoIT編寫的.exe檔案

  /*
     * 上傳檔案
     * 普通上傳:普通的附件上傳是將本地檔案的路徑作為i一個值放在input標籤中,通過form表單將這個值提交給伺服器
     */
    //定義上傳函數,第一個參數browser是瀏覽器的名字,第二個參數filePath是檔案路徑
    public void handleUpload(String browser,String filePath) {
        //定義了autoit.ext檔案的路徑
        String executeFile=System.getProperty("user.dir")+"\\test_exe\\autoit.exe";
        String cmd = "\""+ executeFile+ "\""+ " "+ "\""+ browser+ "\""+ " "+ "\""+ filePath+ "\"";
        System.out.println(cmd);
        try{
            Process process= Runtime.getRuntime().exec(cmd);
            process.waitFor();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

  /**
     * 匯入檔案
     * */
    public void  ExcelImport(String filePath){
        browse_m.click(); //@FindBy(name="get_file")  public WebElement browse_m;//點擊瀏覽按鈕
        handleUpload(DriverManager.browserType,filePath);  
    }

  使用的時候直接調用就可以

 

 

  

 

 

Selenium+java上傳檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.