cppcoder
cppcoder

Reputation: 23145

Avoid download file dialog box in Python/selenium automation

I am using this code to automate a flow which will download a Winzip file without prompt. But it does not seem to work

profile = webdriver.firefox.firefox_profile.FirefoxProfile()
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',("application/zip,
                                                   application/octet-stream"))
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.dir', '/home/jack/DOWNLOAD')
self.driver = webdriver.Firefox(firefox_profile=profile)

I still see the dialog box being opened while automation.

Upvotes: 3

Views: 3774

Answers (1)

cppcoder
cppcoder

Reputation: 23145

I solved this situation by extracting the href of the download link and using the python urllib module.

Using the below code, one can download the file and save it in a different file name as well.

import urllib
url = driver.find_element_by_link_text("Download").get_attribute("href");
urllib.urlretrieve(url, "saveasfilename") 

Upvotes: 3

Related Questions