0

I'm having difficulty downloading a PDF file from a webpage using Selenium 3.8, Firefox 57 and Python 2.7. If I do not manually change the Firefox profile download settings from "Always ask you where to save files" to a specific location, the download pop-up box will still appear and the files will not automatically be downloaded to a specified folder.

I have checked this StackO post and I'm not sure what I am missing. Do I need marionette installed and set up? I do have already set up the geckodriver executable.

Here is my code:

profile = webdriver.FirefoxProfile("/Users/USERNAME/Library/Application Support/Firefox/Profiles/NAME")
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference("browser.helperApps.neverAsk.openFile", 'application/pdf')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
profile.set_preference('browser.download.dir', download_to_location)
browser = webdriver.Firefox(profile)
download_to_location = "PATH_TO_FOLDER_WITHIN_DIRECTORY"

for race in races:
        browser.get(race)
        try:
            [browser.find_element_by_xpath(lxp).click() for lxp in download_from_xpaths]
        except: 
            pass

Screenshot: enter image description here

doyz
  • 887
  • 2
  • 18
  • 43

1 Answers1

0

To automate the downloading of a PDF file to a specified folder you can use the following code block :

newpath = "PATH_TO_FOLDER_WITHIN_DIRECTORY"
if not os.path.exists(newpath):
    os.makedirs(newpath) 
profile = webdriver.FirefoxProfile("/Users/USERNAME/Library/Application Support/Firefox/Profiles/NAME")
profile.set_preference("browser.download.dir",newpath);
profile.set_preference("browser.download.folderList",2);
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain,application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf");
profile.set_preference("browser.download.manager.showWhenStarting",False);
profile.set_preference("browser.helperApps.neverAsk.openFile","text/plain,application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf");
profile.set_preference("browser.helperApps.alwaysAsk.force", False);
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);
profile.set_preference("browser.helperApps.neverAsk.openFile", "");
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);
profile.set_preference("browser.download.manager.closeWhenDone", True);
profile.set_preference("pdfjs.disabled", True);
browser = webdriver.Firefox(profile)
browser.get('Provide_URL_here')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi @DebanjanB, the download pop-up box still appears (causing partial downloaded filed) unless i manually set a specific folder to download to in the Firefox preference settings, meaning that "browser.helperApps.neverAsk.saveToDisk" doesn't seem to have any effect? – doyz Jan 02 '18 at 14:14
  • dianaow, can you share the link from where you are trying to download to see the type of file you are trying to download. – undetected Selenium Jan 02 '18 at 14:16
  • I'm trying to download files from here: https://www.fia.com/events/fia-formula-one-world-championship/season-2016/event-timing-information-2 – doyz Jan 02 '18 at 14:21
  • Which link exactly? – undetected Selenium Jan 02 '18 at 14:22
  • All the laptimes links on the page, but you can test with: Qualifying Session -> Lap Times – doyz Jan 02 '18 at 14:23
  • Try with the updated answer and let me know the status. – undetected Selenium Jan 02 '18 at 16:00
  • @DebajanB Hey, your updated answer did not solve my problem. I tried with ChromeDriver, it did allow saving without pop-up box appearing straight to Downloads folder, but now i can't seem to set options to save to a specific folder – doyz Jan 03 '18 at 05:15