Here is a small code snippet allowing to skip Open/Save us popup downloading a .zip file:

Set Firefox Preference
1
2
3
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");
driver = new FirefoxDriver(profile);

In case we download .csv file:

Set Firefox Preference fo CSV File
1
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");

..or pdf file:

title: Set Firefox Preference for PDF File
1
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");

We can also apply settings for a list of all downloadable files:
(application/... 부분은 엔터없이 "application/x-xpinstall;application/x-zip;application/x-zip-compressed;application/octet-stream;application/zip;application/pdf;application/msword;text/plain;application/octet"같이 붙여 쓸 것)

Set Preferences for All Types of Files
1
2
3
4
5
6
7
8
9
10
profile.SetPreference("browser.download.dir", downloadDir);
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk",
"application/x-xpinstall;
application/x-zip;
application/x-zip-compressed;
application/octet-stream;
application/zip;application/pdf;
application/msword;
text/plain;application/octet");

The best place in code to apply setPreferences of Firefox is @BeforeClass:

Set Preferences for All Types of Files
1
2
3
4
5
6
7
8
9
10
11
12
13
@BeforeClass
public static void initializeClass() throws FileNotFoundException,
          IOException {
profile = new FirefoxProfile();
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");
driver = new FirefoxDriver(profile);
      
properties = new PropertiesUtil(PROPERTIES_FILE);
driver.get(properties.getValue("URL"));
driver.manage().window().maximize();
      
screenshot = new ScreenshotUtil(driver);
}
※ 주의 : setPreference의 ""따움표안에 인자값은 오타를 적을경우 별다른 에러메시지 없이 동작하지 않으니 오타를 내지 않도록 주의

※참고 : MIME types( http://help.dottoro.com/lapuadlp.php )


 http://dataqzone.com/download-file-with-selenium-save-as-popup/

 



Posted by 장안동베짱e :