我尝试设置Firefox以便自动下载文件。我做了enter link description here中的建议,但是无法正常工作。
这是我的代码:
FirefoxOptions options = new FirefoxOptions();
options.SetPreference("browser.download.folderList", 2);
options.SetPreference("browser.download.dir", "C:\\Windows\\temp");
options.SetPreference("browser.download.useDownloadDir", true);
options.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
options.SetPreference("pdfjs.disabled", true); // disable the built-in PDF viewer
options.SetPreference("browser.download.useDownloadDir", true);
driver = new FirefoxDriver(options);
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("https://www.mozilla.org/en-US/foundation/documents");
driver.FindElement(By.LinkText("IRS Form 872-C")).Click();
PDF仍在浏览器PDF查看器中打开。有想法吗?
答案 0 :(得分:0)
要禁用在Firefox中打开和下载pdf:
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList", 2);
options.addPreference("browser.download.dir", downloadPath);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
options.addPreference("pdfjs.enabledCache.state",false);
WebDriver driver = new FirefoxDriver(options);
可以找到here的Mime Tipes列表。
答案 1 :(得分:-1)
我的错误,我没有意识到问题在于C#。
所以我用Java做到了这一点,我认为无论语言如何,硒的功能都非常相似。但是,这里重要的是如何配置FirefoxDriver。
使用硒3.8:
FirefoxProfile profile = new FirefoxProfile();
//if you want to download the file to a different directory than the default
profile.setPreference("browser.download.dir", "dirPath");
//0: the desktop, 1 (default): the downloads folder, 2: the last folder specified for a download
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf;");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("pdfjs.disabled", true);
//The previous configuration can also be done in FirefoxOptions, I did not know and I simply passed the FirefoxProfile object
FirefoxOptions fo = new FirefoxOptions();
fo.setProfile(profile);
FirefoxDriver driver = new FirefoxDriver(fo);
driver.get("http://your.web");
driver.findElement(By.id("download_button")).click();