【发布时间】:2018-07-08 02:49:57
【问题描述】:
我正在使用 Selenium 单击 Firefox 中的链接,我希望它自动下载文件。 我查看了 internet inc 上的各种资源。 Selenium firefox profile for saving a file。 &Set Firefox profile to download files automatically using Selenium and Java。但我无法让它工作。它只是通过打开/保存对话框不断提示我。 我正在使用 Firefox 57.0.2、firefox 驱动程序 3.8.1、Geckodriver 0.19.1。 我可以手动设置 firefox 选项来保存文件,但是在代码中我创建了一个新的配置文件,所以这显然会被忽略。 我的代码是:
System.setProperty(webdriver.gecko.driver",System.getProperty("user.dir") + Constants.GECKODRIVER);
FirefoxOptions firefoxOptions = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", dirPath);
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.helperApps.neverAsk.openFile","application/pdf,text/plain,application/octet-stream,application/x-pdf,application/vnd.pdf,application/vnd.openxmlformats-officedocument.spreadsheethtml,text/csv,text/html,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf,text/plain,application/octet-stream,application/x-pdf,application/vnd.pdf,application/vnd.openxmlformats-officedocument.spreadsheethtml,text/csv,text/html,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel");
profile.setPreference("browser.helperApps.alwaysAsk.force, false);
profile.setPreference("browser.download.manager.useWindow, false);
profile.setPreference("browser.download.manager.focusWhenStarting, false);
profile.setPreference("browser.download.manager.alertOnEXEOpen, false);
profile.setPreference("browser.download.manager.showAlertOnComplete, false);
profile.setPreference("browser.download.manager.closeWhenDone, false);
profile.setPreference("browser.allowpopups, false);
profile.setPreference("pdfjs.disabled",true);
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificatesIssuer(true);
firefoxOptions.setProfile(profile);
currentdriver = new FirefoxDriver();
我试过了
profile.setPreference("plugin.disable_full_page_plugin_for_types","application/pdf,application/x-pdf");
profile.setPreference("plugin.scan.Acrobat,"99.0");
我尝试查看我需要的 2 个文件(.pdf 和 .xlsx)的 MIME 类型,我认为它们是 text/html;charset=iso-8859-1。
请给点建议?
【问题讨论】:
-
您需要做的第一件事是确定这两个文件的确切 mime。其次,您需要删除
browser.helperApps.neverAsk.openFile,因为您不能要求同时打开和保存文件。 -
我试过不使用 .openFile 但没有成功。如何确定 MIME 类型(未连接到 Internet 时)。
-
mime 是请求返回的内容类型。看看这里:stackoverflow.com/questions/36309314/…
-
最新版本的 firefox 与这篇文章略有不同,但是如果我检查元素并转到 Network,Type 是 x-unknown。