【问题标题】:Preferences set to avoid download dialog in firefox is not working for pdf为避免在 Firefox 中下载对话框而设置的首选项不适用于 pdf
【发布时间】:2018-01-30 08:02:51
【问题描述】:

我尝试设置所有可能的首选项以避免在使用 selenium 下载文件时打开和保存文件对话框。

它适用于文本文件,但不适用于 PDF 文件

以下是偏好设置:

    String downloadPath = <some random path>;
    String mimetypes = "application/vnd.pdf,application/vnd.adobe.xfdf,text/csv,application/x-msexcel,application/excel,application/pdf,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml";
    String url = "http://only-testing-blog.blogspot.in/2014/05/login.html";
    FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.download.panel.shown", false);
    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("browser.download.dir", downloadPath);
    profile.setPreference("browser.helperApps.neverAsk.openFile", mimetypes);
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", mimetypes);
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.download.manager.focusWhenStarting", false);
    profile.setPreference("browser.download.manager.useWindow", false);
    profile.setPreference("browser.download.manager.showAlertOnComplete", false);
    profile.setPreference("browser.download.manager.closeWhenDone", false);

    FirefoxDriver driver = new FirefoxDriver(profile);
    driver.get(url);
    driver.findElement(By.xpath("//a[contains(.,'Download PDF File')]")).click();

我正在使用 Firefox 462.53.0 硒版本

请帮助我使它也适用于 PDF、Excel 和 Word 文件

谢谢!

【问题讨论】:

  • PDF 文件发生了什么?
  • 打开或保存文件对话框正在显示 pdf、excel 和工作文件
  • 您能否将问题缩小到PDF, Excel and Word 中的任一文件类型?

标签: selenium selenium-webdriver mime-types selenium-firefoxdriver firefox-profile


【解决方案1】:

我为此找到的一个解决方案是使用 ProfileIni 类。它允许您加载预制的 Firefox 配置文件。你使用如下图

private ProfilesIni init = new ProfilesIni();
private FirefoxProfile profile = init.getProfile("QA");
private WebDriver driver = new WebDriver(profile);

然后,您可以直接在配置文件上设置所有这些首选项,并将设置设置为跳过下载对话框并自动保存到您想要的目标文件夹并保留您需要下载内容的每个测试用例的设置

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-29
    • 2022-01-21
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    相关资源
    最近更新 更多