java+selenium+new——无人化自动下载文件——基于firefox浏览器
我自己的python参考:https://www.cnblogs.com/xiaobaibailongma/p/12078159.html
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("browser.download.manager.showWhenStarting",False) //设置为true时,启动下载时,显示浏览器的文件下载窗口,false时,不显示
fp.setPreference("browser.download.folderList",2) //设置Firefox的默认下载文件夹。0是桌面;1是“我的下载”;2是自定义。
fp.setPreference("browser.download.dir","C:\\Users\\del\\Desktop\\1") //设置自定义路径时,定义的路径
fp.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip") //不询问下载路径;后面的参数为要下载页面的文件类型的值。
fp.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream") //不询问下载路径;后面的参数为要下载页面的文件类型的值。此处为exe文件
===================================================================================================================================
package rjcs; import java.util.*; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.Select; public class xinkaishi { public static void main(String[] args) { System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); //设置火狐的安装路径,防止系统找不到 FirefoxDriver driver = new FirefoxDriver(); //初始化FireFox浏览器实例,并打开浏览器 try { driver.manage().window().maximize(); //最大化窗口 Thread.sleep(5000); FirefoxProfile fp = new FirefoxProfile(); fp.setPreference("browser.download.manager.showWhenStarting",false); fp.setPreference("browser.download.folderList",2); fp.setPreference("browser.download.dir","C:\\Users\\del\\Desktop\\1"); fp.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip"); Thread.sleep(5000); driver.navigate().to("https://www.python.org/downloads/release/python-381/"); Thread.sleep(5000); driver.findElementByLinkText("Windows x86-64 executable installer").click(); Thread.sleep(80000); }catch (Exception e) { e.printStackTrace(); }finally { driver.quit(); } } }