简体   繁体   中英

Selenium WebDriver - Auto Download on Firefox

I try to automate a download from Firefox, using Selenium WebDriver in Java. 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.

In Mozilla>Options>Application, any autorisation is visible, despite the setPreference i added.

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.

I had the same problem short time ago, from your code I see two possibilities:

  1. The MIME type is not in the list:

Check this site if you want a list for each application, maybe you should include the MIME types for excel files too. For example:

application/excel
application/vnd.ms-excel
  1. Disable completely the browser.helperApps option:

Add the following option to your Firefox profile, it will ensure you disable the download dialogue.

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

The application rule which should be appear ( certainly ) : csv automated donwload

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"); 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.

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

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 ,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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