使用 sendKeys(keysToSend) 批量上傳檔案

來源:互聯網
上載者:User

標籤:button   image   style   span   div   上傳   分享   檔案上傳   selenium   

未經允許,禁止轉載!!!

在selenium裡面處理檔案上傳的時候可以使用sendKeys(keysToSend) 上傳檔案

例如: element.sendKeys(“C:\\test\\upload\\test1.txt”)

但是不能使用這種方法一次性批量上傳檔案,如下面的做法是錯誤的!

element.sendKeys(“C:\\test\\upload\\test1.txt”, “C:\\test\\upload\\test2.txt”...)

那麼如何批量上傳檔案呢?其實可以迴圈使用sendKeys(keysToSend) 個方法

如下面的代碼就實現了批量上傳檔案

 /**  *  * @Description: 在百度雲上測試檔案批量上傳功能,主要是通過迴圈的方式去做單一 * 的上傳動作 , 登陸過程已經去掉 */  @Test  public void test_mutilUploadFile() throws Exception {  System.out.println("upload start");          // 擷取上傳控制項元素 WebElement uploadButton = driver.findElement(By.name("html5uploader"));  // 構建上傳檔案路徑,將需要上傳的檔案添加到 CharSequence 數組 CharSequence[] files = new CharSequence[5];          files[0] = "C:\\test\\test1.txt";          files[1] = "C:\\test\\test2.txt";          files[2] = "C:\\test\\test3.txt";          files[3] = "C:\\test\\test4.txt";          files[4] = "C:\\test\\test5.txt";          // 迴圈列出每支需要上傳的檔案路徑,做單一上傳動作         for(CharSequence file: files){  uploadButton.sendKeys(file);          }          Thread.sleep(2000);  System.out.println("upload end");      }

當執行結束後,效果如 :

 

使用 sendKeys(keysToSend) 批量上傳檔案

聯繫我們

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