To download the file you need to configure some parameters in the profile property of Firefox, as in the following code:
PackageCom.test.download;ImportJava.io.File;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.JavascriptExecutor;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.firefox.FirefoxProfile; Public classDownloadtest { Public Static voidMain (string[] args) {firefoxprofile profile=NewFirefoxprofile (); //You can enter about:config in the address bar of the Firefox browser to view the properties//set the download file placement path, note that if the Windows environment must use \ \, do notString Path = "D:\\10-selenium\\workspace\\seleniumtest\\src\\com\\test\\download\\down"; String Downloadfilepath= path + "\\d.exe"; File File=NewFile (Downloadfilepath); if(File.exists ()) {file.delete (); } //Configure response Download ParametersProfile.setpreference ("Browser.download.dir", Path);//Download PathProfile.setpreference ("Browser.download.folderList", 2);//2 is saved at the specified path, 0 represents the default pathProfile.setpreference ("Browser.download.manager.showWhenStarting",false);//whether to show start//Suppress pop-up save box, value is file format, such as ZIP fileProfile.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 ");//about types: You can refer to http://www.w3school.com.cn/media/media_mimeref.aspWebdriver Driver=Newfirefoxdriver (profile); Driver.get ("File:///D:/10-selenium/workspace/SeleniumTest/src/com/test/download/download.html"); Driver.manage (). window (). Maximize (); Driver.findelement (By.linktext (Download) . 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 voidWaitTime (intTime ) { Try{thread.sleep (time); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
Examples of pages to use:
<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>download</title>
<body>
<a href= "D.exe" > Downloads </a>
</body>
To test the code structure:
Selenium Test (Java)-Download file (16)