0

As a first step, I tried to set the default download folder.

I tried 5 options but none of them worked:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Selenium example for downloading a webpage."""

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import os
import time


def main():
    """Download an opened PDF page."""
    browser = get_browser()
    url = "https://martin-thoma.com/pdf/cv-curriculum-vitae.pdf"
    browser.get(url)  # Open a PDF page
    # el = browser.find_element_by_id("plugin")
    time.sleep(5)
    ActionChains(browser).send_keys(Keys.CONTROL, "s").perform()
    print(browser.current_url)
    time.sleep(60)  # Keep the browser open for 60s


def get_browser():
    """Get the browser (a "driver")."""
    # find the path with 'which chromedriver'
    path_to_chromedriver = ('/home/moose/GitHub/algorithms/scraping/'
                            'venv/bin/chromedriver')
    download_dir = "/home/moose/selenium-download/"
    print("Is directory: {}".format(os.path.isdir(download_dir)))

    fail = 6
    options = None
    desired_caps = None
    if fail == 1:
        # Fail (1)
        os.environ['XDG_DOWNLOAD_DIR'] = download_dir
    elif fail == 2:
        # Fail (2)
        options = webdriver.ChromeOptions()
        options.add_argument("download.default_directory={}"
                             .format(download_dir))
    elif fail == 3:
        # Fail (3)
        options = webdriver.ChromeOptions()
        prefs = {"download.default_directory": download_dir}
        options.add_experimental_option("prefs", prefs)
    elif fail == 4:
        # Fail (4)
        desired_caps = {'prefs':
                        {'download': {'default_directory': download_dir,
                                      'directory_upgrade': "true",
                                      'extensions_to_open': ""}}}
    elif fail == 5:
        # Fail (5)
        desired_caps = {'prefs':
                        {'download.default_directory': download_dir}}
    elif fail == 6:
        # Fail (6)
        desired_caps = {'prefs':
                        {'download': {'default_directory': download_dir,
                                      'directory_upgrade': True,
                                      'extensions_to_open': ""}}}

    browser = webdriver.Chrome(executable_path=path_to_chromedriver,
                               chrome_options=options,
                               desired_capabilities=desired_caps)
    return browser


if __name__ == '__main__':
    main()

I know there are simpler ways to download a PDF by URL. However, my real usecase is much more complicated and the download is triggered by a javascript generated click on a link behind a 3-step login process which is purely done with JavaScript.

So this question has two aspects:

  1. How do I change the default download directory with Selenium and Chrome (on Ubuntu 16.04)?
  2. How do I download an opened PDF? (I tried an action chain, but it doesn't work)

I have Google Chrome Version 59.0.3071.115 (Official Build) (64-bit), downloaded via the pip installer.

1 Answer 1

1

First you need

from selenium.webdriver.chrome.options import Options

And change the whole if block and the browser initialization in get_browser() to this:

chrome_options = Options()
chrome_options.add_experimental_option('prefs', {
    "plugins.plugins_list": [{"enabled":False,"name":"Chrome PDF Viewer"}],
    "download": {
        "prompt_for_download": False,
        "default_directory"  : download_dir
    }
})

browser = webdriver.Chrome(path_to_chromedriver, chrome_options=chrome_options)

(I use Windows but there shouldn't be any differences.)

4
  • It is a bit hard to tell what the problem is without seeing your code / actual use case. Is the pdf loaded with the plugin (i.e. it is not embedded and you see the usual download, zoom etc. icons)? Do you log in and open a pdf in one step (I am thinking about the scenario when you click on something you don't have permission to access and a popup...errr...pops up)? Do you have to use chrome driver? Maybe you could use Firefox. Same trick works on that too (obviously with a bit different way to set preferences) and might solve your problem. Aug 18, 2017 at 10:12
  • (1) I see the usual download / zoom icons. (2) I've just realized it is "Chromium Version 60.0.3112.78" on the other machine. I'll fix that. Aug 18, 2017 at 10:21
  • But it does also not work when I add chrome_options.add_argument("--headless") to chrome :-( Aug 18, 2017 at 11:44
  • I have no ide why it doesn't work so let's see a bit of a hack. What if you print the pdf to a pdf? You can add arguments to print to pdf and to hide the preview of the page about to be printed although you would have to deal with the save prompt. Aug 18, 2017 at 12:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.