gpt4 book ai didi

java - 在 Java 中使用 Selenium WebDriver 下载 zip 文件 - 下载弹出窗口总是出现

转载 作者:行者123 更新时间:2023-11-30 08:59:52 24 4
gpt4 key购买 nike

我使用以下代码从网站下载文件,我使用的是 Firefox 32.0.3 和版本为 2.43 的 Selenium jar。

    FirefoxProfile  firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
firefoxProfile.setPreference("browser.download.dir","C:\\RDM_Files");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip;application/octet-stream;application/x-zip;application/x-zip-compressed");
firefoxProfile.setPreference("plugin.disable_full_page_plugin_for_types", "application/zip");

WebDriver driver = new FirefoxDriver(firefoxProfile);

我检查了正在下载的文件的 MIME 类型是 application/zip。每次我尝试下载文件时,都会出现一个窗口,询问是打开文件还是保存文件。

我搜索了 Stackoverflow.com 并找到了与处理 .pdf 文件而非 zip 文件相关的帖子。请帮忙

最佳答案

我想你正在寻找这样的东西

//common to all the cases
FirefoxProfile prof = new FirefoxProfile();

//Case:1 - Use this case to set download this code to your browser's default location
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//Case:2 - Download file to Desktop
//prof.setPreference("browser.download.folderList", 0);
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//Case:3 - Download to custom folder path. Replace d:\\selenium with your Download Location
prof.setPreference("browser.download.dir","D:\\selenium\\");
prof.setPreference("browser.download.folderList", 2);
prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//This will work for all cases mentioned above
WebDriver driver = new FirefoxDriver(prof);
driver.get("http://docs.seleniumhq.org/download/");
driver.findElement(By.xpath("//tr[1]/td[4]/a[text()='Download']")).click();

关于java - 在 Java 中使用 Selenium WebDriver 下载 zip 文件 - 下载弹出窗口总是出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27047044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com