selenium测试(Java)--上传文件(十五)

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

原文地址:https://www.cnblogs.com/moonpool/p/5675970.html