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();
			}
		  
}

一定要确定 下载文件的Content-type是什么,如下图所示(在firefox中右键查看),请求头出错虽然脚本会执行成功,但是文件不会下载
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值