目的:下载链接的时候,通常需要人为地设定下载文件保存的路径,这样就无法实现全自动化执行下载过程。
被测试网页的网址:http://ftp.mozilla.org/pub/firefox/releases/35.0b8/win32/zh-CN/
实例代码:
package cn.om.webdriverapi;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.AfterMethod;
public class TestAutoFileDownload {
public static String downloadFilePath = "C:\\Users\\oumin1\\下载";
WebDriver driver;
String baseURL;
@Test
public void testFileDownload() {
driver=new FirefoxDriver(FirefoxDriverProfile());
driver.get(baseURL);
//单击包含“Stub”关键字的下载链接
driver.findElement(By.partialLinkText("Stub")).click();
//设定10秒中的延迟,让程序下载完成。如果网络下载