在使用Selenium在FireFox中设置首选项时,请使用MIME类型 [英] Issue with mime type when setting preferences in FireFox with Selenium

查看:290
本文介绍了在使用Selenium在FireFox中设置首选项时,请使用MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我几个星期以来一直在苦苦挣扎的一个问题,但似乎找不到一个可行的解决方案。在坚果外壳,我的程序去一个网站,并下载一些文件。当它下载文件时,FireFox总是显示弹出式窗口,询问我要保存的位置。为了摆脱这个弹出窗口,我创建了一个配置文件首选项。

  profile.setPreference(browser.helperApps.neverAsk .saveToDisk, 应用程序/压缩,应用/ vnd.ms-EXCEL); 

此首选项适用于我需要下载的一个zip文件,但对于其他3我需要的Microsoft CSV文件。我已经尝试了所有我能想到的适用的MIME类型,但都没有工作。我通过一个在线程序运行我的文件,它应该告诉你这个文件是什么类型的文件,它宣称我测试的文件是一个 application / vnd.ms-excel 。不幸的是,我从来没有能够得到这种类型的工作。我尝试了各种不同的方式来设置配置文件,例如:

  profile.setPreference(browser.helperApps.neverAsk。 saveToDisk,application / zip+application / vnd.ms-excel); 

甚至只要使用 application / vnd.ms-excel 没有在它前面的 zip 类型,仍然没有骰子。有没有另一种方法来设置这个我没有那个窗口弹出每次我尝试下载这些文件?更好的是,我的代码有什么问题可以让zip文件起作用,但是不能用微软的csv?解决方案

这里有一组特定于CSV的MIME类型,它们可以在SO上的其他用户使用:

  profile.setPreference('browser .helperApps.neverAsk.saveToDisk',text / plain,application / vnd.ms-excel,text / csv,application / csv,text / comma-separated-values,application / download,application / octet-stream,binary / octet ),

我很确定 application / x-unknown 是一个适合你的工具,因为这是Firefox本身确定为你的文件的MIME类型(为我工作)。 b
$ b

您也可以执行以下操作:


  • 使用Firefox手动下载此特定文件

  • 当保存文件弹出窗口被打开时,检查自动为文件l执行此操作ike this now now复选框并保存文件现在,转到帮助 - >疑难解答信息
  • 找到配置文件目录并保存路径在您的Selenium代码中,使用 FirefoxProfile 指向此现有配置文件启动Firefox,请参阅更多关于如何执行此操作的信息。
  • a href =https://stackoverflow.com/questions/6787095/how-to-stop-selenium-from-creating-temporary-firefox-profiles-using-web-driver>这里

This is a problem I've been struggling with for a few weeks now, but can't seem to find a viable solution. In a nut shell, my program goes to a website and downloads a handful of files. When it goes to download a file, FireFox always displays the popup asking where I want to save it to. In an attempt to get rid of this popup, I created a profile preference.

profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip,application/vnd.ms-excel");

This preference works great with the one zip file I need to download, but it's completely ignored for the other 3 Microsoft CSV files that I need. I have tried every applicable mime type I can think of and none of them work. I ran my file through an online program that is supposed to tell you what mime type the file is, and it proclaimed the file I tested was a application/vnd.ms-excel. Unfortunately though, I have never been able to get this type to work. I have tried various different ways of setting up the profile, such as:

profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip" + "application/vnd.ms-excel");

And even just using application/vnd.ms-excel without the zip type in front of it, and still no dice. Is there another way to set this up to where I don't have that window popup each time I try and download these files? Better yet, is there something wrong with my code that would allow the zip file to work, but not the Microsoft csv?

解决方案

Here is a set of CSV-specific mime-types that worked for other users here on SO:

profile.setPreference('browser.helperApps.neverAsk.saveToDisk', "text/plain, application/vnd.ms-excel, text/csv, application/csv, text/comma-separated-values, application/download, application/octet-stream, binary/octet-stream, application/binary, application/x-unknown")

I'm pretty sure that the application/x-unknown is the one that would work for you since this is what Firefox itself determines as your file's mime-type (worked for me).

You may also do the following:

  • download this specific file manually with Firefox
  • when the Save File popup would be opened, check the "do this automatically for files like this from now on" checkbox and save the file
  • now, go to Help -> Troubleshooting Information
  • find the profile directory and save the path to it
  • in your Selenium code, start Firefox with the FirefoxProfile pointing to this existing profile, see more about how to do it here

这篇关于在使用Selenium在FireFox中设置首选项时,请使用MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆