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

1. 当页面中是通过input标签实现上传功能时,可以使用selenium来上传功能。

如下:

package com.test.upload;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class UploadTest {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/upload/upload.html");
        driver.manage().window().maximize();

        String path = System.getProperty("user.dir") + "\\src\\com\\test\\upload\\upload.html";
        System.out.println(path);
        File file = new File(path);

        if (file.exists()) {
            //找到input,然后利用sendKeys来上传文件
            driver.findElement(By.cssSelector("#uploadFIle")).sendKeys(file.getPath());
            System.out.println(file.getPath());
        }

        driver.quit();
    }

}

2. 如果网页中的上传功能不是使用input来实现,那就需要使用其他方法来实现模拟

可以使用AutoIt录制脚本实现:

使用方法参考:

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

工具下载地址:

官网:https://www.autoitscript.com/site/autoit/downloads/
网盘:http://pan.baidu.com/s/1cievQe

posted @ 2017-08-05 11:07  李清欣  阅读(1623)  评论(0编辑  收藏  举报