1. 當頁面中是通過input標簽實現上傳功能時,可以使用selenium來上傳功能。
如下:
1 package com.test.upload; 2 3 import java.io.File; 4 5 import org.openqa.selenium.By; 6 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.firefox.FirefoxDriver; 8 9 public class UploadTest { 10 11 public static void main(String[] args) { 12 WebDriver driver = new FirefoxDriver(); 13 driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/upload/upload.html"); 14 driver.manage().window().maximize(); 15 16 String path = System.getProperty("user.dir") + "\\src\\com\\test\\upload\\upload.html"; 17 System.out.println(path); 18 File file = new File(path); 19 20 if (file.exists()) { 21 //找到input,然后利用sendKeys來上傳文件 22 driver.findElement(By.cssSelector("#uploadFIle")).sendKeys(file.getPath()); 23 System.out.println(file.getPath()); 24 } 25 26 driver.quit(); 27 } 28 29 }
2. 如果網頁中的上傳功能不是使用input來實現,那就需要使用其他方法來實現模擬
可以使用AutoIt錄制腳本實現:
使用方法參考:
http://www.cnblogs.com/fnng/p/4188162.html
工具下載地址:
官網:https://www.autoitscript.com/site/autoit/downloads/
網盤:http://pan.baidu.com/s/1cievQe