How to perform selenium download?

169    Asked by AlisonKelly in QA Testing , Asked on May 10, 2022

Basically I want to at least check that a download-able file exists / download link works and preferably get stuff like the file size too.

Here's an example:

link = self.browser.find_element_by_link_text('link text')
href = link.get_attribute('href')
download = self.browser.get(href)
print download
That fourth line prints "None", presumably because I haven't manually clicked the Save button, and even if I had, I doubt WebDriver would be able to "see" the file.

Any ideas? I'm using Firefox as my browser-under-test, and I understand that the file handling for downloads is somewhat browser and/or OS-specific.

Answered by Al German

Here's a solution for selenium download. Set Firefox's preferences to save automatically, and not have the downloads window popup. Then you just grab the file, and it'll download.


So, something like this:
FirefoxProfile Profile = new FirefoxProfile();
fxProfile.setPreference("browser.download.folderList",2);
fxProfile.setPreference("browser.download.manager.showWhenStarting",false);
fxProfile.setPreference("browser.download.dir","c:\mydownloads");
fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");
WebDriver driver = new FirefoxDriver(fxProfile);
driver.navigate().to("http://www.foo.com/bah.csv");

and given you now have the download directory, never ask to save, and no download manager appearing, automation from this point should be straightforward.



Your Answer

Interviews

Parent Categories