简体   繁体   English

Selenium WebDriver-在Firefox上自动下载

[英]Selenium WebDriver - Auto Download on Firefox

I try to automate a download from Firefox, using Selenium WebDriver in Java. 我尝试使用Java中的Selenium WebDriver自动从Firefox下载。 Unfortunately, i have found a lot of answers but this is strangly not working in my code. 不幸的是,我找到了很多答案,但这在我的代码中无法正常工作。

I tried 我试过了

profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.dir", "D:\\");
profile.setPreference("browser.helperApps.neverAsk.openFile","application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");

but impossible to work! 但无法工作! The type of the file i try to download is a CSV Excel file. 我尝试下载的文件类型是CSV Excel文件。

In Mozilla>Options>Application, any autorisation is visible, despite the setPreference i added. 在Mozilla>选项>应用程序中,尽管我添加了setPreference,但是任何自动提升都是可见的。

Just before that, i just added 在此之前,我刚刚添加了

profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "XXX.XXX.XXX.XXX");
profile.setPreference("network.proxy.http_port", XXXX);

cause i have a proxy in my company, but this time in Mozilla>Options>Internet Settings, the proxy rules i added are visible. 因为我在公司中有一个代理,但是这次在Mozilla>选项> Internet设置中,可见我添加的代理规则。

I had the same problem short time ago, from your code I see two possibilities: 不久前我遇到了同样的问题,从您的代码中我看到了两种可能性:

  1. The MIME type is not in the list: MIME类型不在列表中:

Check this site if you want a list for each application, maybe you should include the MIME types for excel files too. 如果需要每个应用程序的列表,请访问此站点 ,也许您还应该包括excel文件的MIME类型。 For example: 例如:

application/excel
application/vnd.ms-excel
  1. Disable completely the browser.helperApps option: 完全禁用browser.helperApps选项:

Add the following option to your Firefox profile, it will ensure you disable the download dialogue. 在您的Firefox个人资料中添加以下选项,这将确保您禁用下载对话框。

profile.set_preference('browser.helperApps.alwaysAsk.force', False)

Thank you a lot for your answer, I saw I'm not alone with this problem, but it seems to work often for people... I tried to add your code, but no difference. 非常感谢您的回答,我看到我并不孤单地遇到这个问题,但是它似乎经常对人们有用……我试图添加您的代码,但是没有区别。

Maybe it's possible the problem comes from a computer setting? 也许问题可能来自计算机设置?

The popup which continues to appear : Download Popup Firefox 弹出窗口继续出现: 下载Firefox弹出窗口

The application rule which should be appear ( certainly ) : csv automated donwload 应该出现的应用程序规则(当然): csv自动下载

By the way, any rule in application options appears with the famous profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream, application/excel, application/vnd.ms-excel"); 顺便说一句,应用程序选项中的任何规则都以著名的profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream, application/excel, application/vnd.ms-excel"); I dont know if it should. 我不知道是否应该。

The last idea I got is to create manually a profile and get it when you start a firefox browser, but i dont really know if its possible, knowing the application im making have to work on any computer connected to the VPN. 我得到的最后一个想法是手动创建一个配置文件,并在启动firefox浏览器时获取它,但是我真的不知道它是否可行,因为我知道该应用程序必须能够在连接到VPN的任何计算机上工作。

Thank again for your help, and if anybody thinks having a solution, it would be great! 再次感谢您的帮助,如果有人认为有解决方案,那就太好了!

In this Case without in different way, Create Firfox profile By enter into Run Command 在这种情况下,请以不同方式创建Firfox配置文件,方法是运行命令

firefox.exe -P

It will ask you to create new profile and start that profile(Refer https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles ) , manualy try to download the file , when the popups came just check automatic download , 它将要求您创建新的配置文件并启动该配置文件(请参阅https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles ),手动尝试下载文件,当弹出窗口出现时,只需检查自动下载,

Then add the Profile in your selenium code 然后将配置文件添加到您的硒代码中

<profile_name> = webdriver.FirefoxProfile()
driver = webdriver.Firefox(<profile_name>)
driver.get("http://google.com")

Try to Run the code now it will automatically download the content without any popup. 现在尝试运行代码,它将自动下载内容,而不会弹出任何窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM