【问题标题】:Cannot auto-save file with Selenium Firefox无法使用 Selenium Firefox 自动保存文件
【发布时间】: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。

标签: selenium firefox


【解决方案1】:

我在 selenium 的 webdriver.FirefoxProfile 中使用 set_preference 也没有成功。有效的是修改 firefox 配置文件目录中的 handlers.json 文件。

必须在 json 文件的 mimeTypes 下插入以下内容:

"application/text":{"action":0,"extensions":["csv"]},

【讨论】:

    【解决方案2】:

    是的,我不擅长英语,但我对这个该死的问题感到愤怒,所以写下这个答案。我在 firefox 配置文件目录中的 set_preference 和 json 也没有成功。如你所知,我们的生命是短暂的。所有问题都必须快速解决。所以我在下面使用丑陋的解决方案并解决了。

    我再说一遍,我们的生命很短暂。如果这个该死的丑陋解决方案对您有好处,请使用它并随意享受我们的生活。

    from ahk import AHK
    ahk = AHK()
    
    sleep(2)
    handle = ahk.win_get(title="ahk_class MozillaDialogClass") # yeah this is f***ing file download dialog window of firefox
    if  ("" !=handle.title):
        handle.send("{Down}")
        sleep(0.2)
        handle.send("{Tab}{Tab}")
        sleep(0.2)
        handle.send("{Space}")
        sleep(0.2)
        handle.send("{Tab}{Enter}")
    

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 2015-08-06
      • 2014-01-20
      相关资源
      最近更新 更多