(每当看见浏览量加一 我就知道这世界上知道我是个小辣鸡的人又多了一个;)
下载该链接的某个文件:http://ftp.mozilla.org/pub/firefox/releases/35.0b8/win32/zh-CN/
profile.setPreference("browser.download.folderList",2)
若没有设定,一般默认为1 ,下载至下载文件夹,2,下载至指定文件夹,0,下载至用户桌面
//自动化下载某个文件
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
public class DownloadFiles {
public static String DownloadFilepath = "D:\\DownloadFiles";
WebDriver driver;
String url;
JavascriptExecutor js;
@Test
public void test() throws Exception
{
driver = new FirefoxDriver(FilefoxDriverProfile());
driver.get(url);
driver.findElement(By.partialLinkText("Stub")).click();
try
{
Thread.sleep(3000);
}catch(Exception e){
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod()
{
url="http://ftp.mozilla.org/pub/firefox/releases/35.0b8/win32/zh-CN/";
}
@AfterMethod
public void afterMethod()
{
driver.quit();
}
public static FirefoxProfile FilefoxDriverProfile () throws Exception
{
//声明一个profile对象
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.dir", DownloadFilepath);
profile.setPreference("browser.helperApps.neverAsk.openFile",
"application/xhtml+xml,application/xml,application/x-msdownload,application/octet/octet-stream,application/exe,txt/csv,application/pdf,application/x-msexcl,application/x-excel,application/excel,image/png,image/jpeg,text/html,text/plain,text/x-c");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/xhtml+xml,application/xml,application/x-msdownload,application/octet/octet-stream,application/exe,txt/csv,application/pdf,application/x-msexcl,application/x-excel,application/excel,image/png,image/jpeg,text/html,text/plain,text/x-c");
//不会打开未知MIMe类型
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
//不会弹出警告框
profile.setPreference("browser.download.manager.alertOnEXEopen", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.closewhenDone", false);
return profile;
}
}