selenium测试(Java)--下载文件(十六)
下载文件需要在Firefox 的profile属性中配置一些参数,如下面的代码:
package com.test.download; import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile; public class DownloadTest { public static void main(String[] args) { FirefoxProfile profile = new FirefoxProfile(); // 可以在Firefox浏览器地址栏中输入about:config来查看属性
// 设置下载文件放置路径,注意如果是windows环境一定要用\\,用/不行
String path = "D:\\10-selenium\\workspace\\SeleniumTest\\src\\com\\test\\download\\down";
String downloadFilePath = path + "\\d.exe";
File file = new File(downloadFilePath);
if (file.exists()) {
file.delete();
} // 配置响应下载参数
profile.setPreference("browser.download.dir", path);// 下载路径
profile.setPreference("browser.download.folderList", 2);// 2为保存在指定路径,0代表默认路径
profile.setPreference("browser.download.manager.showWhenStarting", false);// 是否显示开始
// 禁止弹出保存框,value是文件格式,如zip文件
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/zip,text/plain,application/vnd.ms-excel,text/csv,text/comma-separated-values,application/octet-stream,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document");
//关于类型:可以参考http://www.w3school.com.cn/media/media_mimeref.asp WebDriver driver = new FirefoxDriver(profile);
driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/download/download.html");
driver.manage().window().maximize(); driver.findElement(By.linkText("下载")).click(); waitTime(3000);
String js_exist = "alert(\"download successfully\")";
String js_not_exist = "alert(\"download unsuccessfully\")"; if (file.exists()) {
((JavascriptExecutor) driver).executeScript(js_exist);
} else {
((JavascriptExecutor) driver).executeScript(js_not_exist);
} waitTime(5000);
// driver.quit(); } static public void waitTime(int time) { try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
使用到的页面例子:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>download</title>
</head>
<body>
<a href="d.exe">下载</a>
</body>
</html>
测试代码结构:

selenium测试(Java)--下载文件(十六)的更多相关文章
- 201871010106-丁宣元 《面向对象程序设计(java)》第十六周学习总结
201871010106-丁宣元 <面向对象程序设计(java)>第十六周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://home.cnblogs.com/u/nw ...
- Java IO(十六)InputStreamReader 和 InputStreamWriter
Java IO(十六)InputStreamReader 和 InputStreamWriter 一.介绍 InputStreamReader 和 OutputStreamWriter 是字节流通向字 ...
- Java进阶(三十六)深入理解Java的接口和抽象类
Java进阶(三十六)深入理解Java的接口和抽象类 前言 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太 ...
- Selenium+Python:下载文件(Firefox 和 Chrome)
引自 https://blog.csdn.net/Momorrine/article/details/79794146 1. 环境 操作系统 Win10 IDE Eclipse (Oxyg ...
- 【文件下载】Java下载文件的几种方式
[文件下载]Java下载文件的几种方式 摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...
- “全栈2019”Java多线程第二十六章:同步方法生产者与消费者线程
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第十六章:同步synchronized关键字详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java异常第十六章:Throwable详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- 201671010140. 2016-2017-2 《Java程序设计》java学习第十六周
java学习第十六周-并发 本周,学习了Java中线程,并发的知识,在老师的带领下,进行了对知识的理解学习,以及对实验的运行讲解,对这一块内容掌握的还可以,在自主编程中,也能够完成.线, ...
- “全栈2019”Java第七十六章:静态、非静态内部类访问权限
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
随机推荐
- VirtualBOX 不能mount优盘,移动硬盘解决方案
The Solution (basically nayasis' solution with the second driver installation added): Safely unplug ...
- mysql 一对多 group查询
场景:查询所有A表的数据,并且关联B表,再数据集中插入一个COUNT列.该列的数据是B表的COUNT 扩展:假设join的表数据为空,但我任然要把所有表A的数据取出来,那就用LEFT JOIN SEL ...
- unity5 Edit Collider
按下Edit Collider按钮,视图中Collider线框中出现控制点,可以通过拖动控制点对Collider进行调整.
- 将linux下的rm命令改造成mv到指定的目录下
rm是Linux下文件删除的命令,它是Linux下非常强大却又非常危险的一条命令,特别是rm -rf有时候强大到让你欲哭无泪,当你想清除当前目录下的所有文件和目录时,很简单#rm -rf ./*这没什 ...
- [转]WCF类型共享技巧
调用过WCF服务的同学可能都会遇到这样的问题,同一个实体类型,不同的服务Visual Studio生成了不同的版本,例如Service1.User和Service2.User,对于C#来说,这是两个不 ...
- Is "UNION ALL" Always Better Than "UNION"? Watch Out!
无论是教科书还是平常的实践都告诉我们 - “尽量避免用UNION,尽可能用UNION ALL替代”. 原因很简单,UNION会对结果集进行排序去重操作,这是一个很消耗资源的操作. 但是,今天碰到了一个 ...
- ubuntu 14.04英文环境设置成中文
适用于ubuntu 14.04英文版的系统,其它版本的设置应该是大同小异的. 进入ubuntu系统,在顶部齿状标志找到system... 2.在personal找到Language Support 3 ...
- 每日英语:Burning Question / Does Reading In Dim Light Hurt Your Eyes?
Mom always told us we'd go blind if we read in the dark. Does science back her up? Jim Sheedy, a doc ...
- zend studio 安装后一体化配置
1.安装语言包http://www.eclipse.org/babel/downloads.php 11.0的时候仍然是junohttp://download.eclipse.org/technolo ...
- c++ 使用vs2010调用 win32api
以前读书时都是用vc6.0.后来学c#用vs.装系统只装了vs2010.今天用vs2010写c++程序.发现有点陌生.就总结下,免得以后忘记. 首先用vs2010选择c++语言.新建一个win32控制 ...