Ok so far i have my programing going to the website i want to download link from and selecting it, then the firefox dialogue box shows up and i don’t know what to do. i want to save this file to a folder on my desktop. I am using this for a nightly build so i need this to work. Please help.
Here is my code that grabs the download link from the website:
driver = web driver.Firefox()
driver.implicitly_wait(5)
driver.get("Name of web site I'm grabbing from")
driver.find_element_by_xpath("//a[contains(text(), 'DEV.tgz')]".click()
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You need to make Firefox save this particular file type automatically.
This can be achieved by setting browser.helperApps.neverAsk.saveToDisk preference:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", 'PATH TO DESKTOP')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("Name of web site I'm grabbing from")
driver.find_element_by_xpath("//a[contains(text(), 'DEV.tgz')]").click()
More explanation:
browser.download.folderListtells it not to use defaultDownloadsdirectorybrowser.download.manager.showWhenStartingturns of showing download progressbrowser.download.dirsets the directory for downloadsbrowser.helperApps.neverAsk.saveToDisktells Firefox to automatically download the files of the selectedmime-types
You can view all these preferences at about:config in the browser. There is also a very detailed documentation page available here: About:config entries.
Besides, instead of using xpath approach, I would use find_element_by_partial_link_text():
driver.find_element_by_partial_link_text("DEV.tgz").click()
Also see:
- Access to file download dialog in Firefox
- Firefox + Selenium WebDriver and download a csv file automatically
Method 2
If the application is generated dynamically (mime-types) using Chrome browser will be a better approach since the Chrome will not open the file download pop-up.But multiple download option should be enabled if you need multiple downloads.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0