您的位置:首页 > 其它

Selenium学习笔记(四)——上传&下载文件

2017-07-26 12:25 351 查看

上传文件

上传文件的页面通常如下:



上传文件步骤:

1. 获取文件路径输入框。

2. 调用sendKeys()方法,输入上传文件的路径。

2. 点击上传按钮。

注意

不需要模拟点击“选择文件”按钮,因为其弹出窗口是系统窗口,非HTML页面,Selenium不能对其操作。

下载文件

同样,Selenium也不能操作“另存为”这种系统窗口。在这种情况下,我们只能通过脚本调用Wget的方式来实行下载。

Wget下载路径:https://eternallybored.org/misc/wget/

driver.get(baseUrl);
//获取下载连接元素
WebElement downloadButton = driver.findElement(By.id("messenger-download"));
//获取下载连接URL
String sourceLocation = downloadButton.getAttribute("href");
//拼接Wget下载命令
String wget_command = "cmd /c C:\\Wget\\wget.exe -P D: --no-check-certificate " + sourceLocation;

try {
//执行命令
Process exec = Runtime.getRuntime().exec(wget_command);
int exitVal = exec.waitFor();
System.out.println("Exit value: " + exitVal);
} catch (InterruptedException | IOException ex) {
System.out.println(ex.toString());
}
//TODO:检查下载是否成功
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  selenium