Selenium 实现自动下载文件(FirefoxOptions,FirefoxProfile) - 根据Selenium Webdriver3实战宝典
Firefox 版本是72
geckodriver 是 0.24
selenium 是3.14
代码中注释有关于FirefoxOptions,FirefoxProfile的解释,请各位寻找一下,不做另外解释
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.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile; import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; public class TestDemo {
//设定存储下载文件的路径
public static String downloadFilePath = "C:\\downloadFiles"; WebDriver driver;
String baseUrl;
JavascriptExecutor js; @BeforeMethod
public void befordMethod(){
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Administrator\\Desktop\\geckodriver.exe");
//baseUrl = "http://ftp.mozilla.org/pub/firefox/releases/56.0/win64/zh-CN/";
baseUrl = "http://ftp.mozilla.org/pub/firefox/releases/35.0b8/win32/zh-CN/";
} @Test
public void testDownloadFile() throws Exception{
driver = new FirefoxDriver(firefoxDriverOptions());
//driver = new FirefoxDriver();
driver.get(baseUrl);
//单机包含 Stub关键字的下载链接
driver.findElement(By.partialLinkText("Stub")).click();
//driver.findElement(By.xpath("/html/body/table/tbody/tr[3]/td[2]/a")).click();
//设定10秒延迟,让程序下载完成,如果网络下载很慢,可以根据预估的下载完成时间
//设定暂停时间 //TODO 是否可以 用一个循环,来判定是否 下载完成,找到下载的元素,是否出现“已下载”,如果出现这个 partialLinkText 那么久break try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static FirefoxOptions firefoxDriverOptions(){ try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FirefoxOptions options = new FirefoxOptions(); //声明一个profile对象
FirefoxProfile profile = new FirefoxProfile();
//设置Firefox的“broswer.download.folderList”属性为2
/**
* 如果没有进行设定,则使用默认值 1,表示下载文件保存在“下载”文件夹中
* 设定为0,则下载文件会被保存在用户的桌面上
* 设定为2,则下载的文件会被保存的用户指定的文件夹中
*/
profile.setPreference("browser.download.folderList", 2); //browser.download.manager.showWhenStarting的属性默认值为true
//设定为 true , 则在用户启动下载时显示Firefox浏览器的文件下载窗口
//设定为false,则在用户启动下载时不显示Firefox浏览器的文件下载窗口
profile.setPreference("browser.download.manager.showWhenStarting", false);
//设定文件保存的目录
profile.setPreference("browser.download.dir", downloadFilePath);
//browser.helperApps.neverAsk.openFile 表示直接打开下载文件,不显示确认框
//默认值.exe类型的文件,"application/excel"表示Excel类型的文件 // application/x-msdownload
profile.setPreference("browser.helperApps.neverAsk.openFile", "application/x-msdownload");
//browser.helperApps.never.saveToDisk 设置是否直接保存 下载文件到磁盘中默认值为空字符串,厦航代码行设定了多种温江的MIME类型
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/x-msdownload"); //browser.helperApps.alwaysAsk.force 针对位置的MIME类型文件会弹出窗口让用户处理,默认值为true ,设定为false 表示不会记录打开未知MIME类型文件
profile.setPreference("browser.helperApps.alwaysAsk.force", true); //下载.exe文件弹出窗口警告,默认值是true ,设定为false 则不会弹出警告框
profile.setPreference("browser.download.manager.alertOnEXEOpen", false); //browser.download.manager.focusWhenStarting设定下载框在下载时会获取焦点
profile.setPreference("browser.download.manager.focusWhenStarting", true); //browser.download.manager.useWindow 设定下载是否现在下载框,默认值为true,设定为false 会把下载框隐藏
profile.setPreference("browser.download.manager.useWindow", false); //browser.download.manager.showAlertOnComplete 设定下载文件结束后是否显示下载完成的提示框,默认值为true,
//设定为false表示下载完成后,现在下载完成提示框
profile.setPreference("browser.download.manager.showAlertOnComplete", false); //browser.download.manager.closeWhenDone 设定下载结束后是否自动关闭下载框,默认值为true 设定为false 表示不关闭下载管理器
profile.setPreference("browser.download.manager.closeWhenDone", false); try {
Thread.sleep(80000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} options.setProfile(profile); return options; } @AfterMethod
public void afterMethod(){
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.quit();
}

Selenium 实现自动下载文件(FirefoxOptions,FirefoxProfile) - 根据Selenium Webdriver3实战宝典的更多相关文章
- selenium实现自动下载文件
#coding:utf-8'''说明:导出'''from selenium import webdriverfrom public.highlightElement import highlightf ...
- Selenium+Python:下载文件(Firefox 和 Chrome)
引自 https://blog.csdn.net/Momorrine/article/details/79794146 1. 环境 操作系统 Win10 IDE Eclipse (Oxyg ...
- 转:Windows下用sftp自动下载文件
远程服务器是Linux操作系统,没有ftp服务,可以ssh,数据库每天2:00会自动创建一个备份文件,本地计算机是windows操作系统,希望用sftp每天3:00下载远程服务器上的备份文件.本地系统 ...
- 记号一下selenium+Firefox自动下载的参数
参考: https://blog.csdn.net/wxstar8/article/details/80782556 https://blog.csdn.net/xiaoguanyusb/articl ...
- 调用百度云Api实现从百度云盘自动下载文件
一.注册账号 要从百度云下载文件,首先,注册一个百度云账号,现在可能都要注册手机号啦,当然,如果你已经注册过,很幸运,就可以省略掉此步骤啦. 如图登录后所示: 点击Access Key,即显示上面的图 ...
- 转 selenium 自动下载文件
#coding=utf-8from selenium import webdriver #实例化一个火狐配置文件fp = webdriver.FirefoxProfile() #设置各项参数,参数可以 ...
- selenium 自动下载文件
#coding=utf-8 from selenium import webdriver #实例化一个火狐配置文件 fp = webdriver.FirefoxProfile() #设置各项参数,参数 ...
- linux crontab命令 自动下载文件
#crontab -e#download stock data, Mon-Fri, 9:15 - 11:30, 13:00 - 15:0015,30,40,50 9 * * 1-5 (cd /home ...
- C#异步下载文件--基于http请求
1.废话不多说,直接上代码: using System; using System.IO; using System.Net; namespace AsyncProgram { class Progr ...
随机推荐
- JdbcRDD连接MySQL
(1)添加依赖 <dependencies> <dependency> <groupId>org.apache.spark</groupId> < ...
- js几个常用的弹层
js弹层技术很常见,自己每次用上网找,一找一大堆. 对比了几种,考虑通用性和易用性,这里记录两个. jQueryUI的http://jqueryui.com/dialog/#modal-form ar ...
- day19-3个双下item方法
#使用双下item方法来实现属性的增删改查: # 查:__getitem__ 增改:__setitem__ 删除: __delitem__ class Goods: def __init__(self ...
- 4)在url中加上a分发参数,用来选哪一个函数
文件关系目录展示: 然后代码改动部分展示: zixun.controller.class.php <?php //header('Content-type:text/html;charset=u ...
- 信息熵、信息增益、信息增益率、gini、woe、iv、VIF
整理一下这几个量的计算公式,便于记忆 采用信息增益率可以解决ID3算法中存在的问题,因此将采用信息增益率作为判定划分属性好坏的方法称为C4.5.需要注意的是,增益率准则对属性取值较少的时候会有偏好,为 ...
- python数据类型:元组
python数据类型:元组 python的元组与列表类似,但是元组的元素不能修改 元组使用小括号,列表使用大括号 元组创建简单,只需要在括号中添加元素,使用逗号隔开 创建元组: tup1 = (50, ...
- python项目中对mysql数据库进行配置,并进行连接测试
在settings.py中配置mysql数据库进行相关配置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME ...
- Margin of Error|sample size and E
8.3 Margin of Error 由该公式可知: To improve the precision of the estimate, we need to decrease the margin ...
- 基于ci框架 修改出来了一个带农历的万年历。
1这里没有写model:代码一看就懂,没什么负杂地方,就是麻烦一点. 直接control模块的代码: <?php if ( ! defined('BASEPATH')) exit('No dir ...
- dotfuscator安装
1.vs 2017 安装 dotfuscator 组件 打开vs 2017 按 ctrl + Q在输入框中输入“dotfuscator” ,选中第一个. 2.安装完成后即可在vs的工具中看到该组件 3 ...