title: How can I download something with Selenium and Chrome?
body: <p>As a first step, I tried to set the default download folder.</p>
  
  <p>I tried 5 options but none of them worked:</p>
  
  <pre><code>#!/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()
  </code></pre>
  
  <p>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.</p>
  
  <p>So this question has two aspects:</p>
  
  <ol>
  <li>How do I change the default download directory with Selenium and Chrome (on Ubuntu 16.04)?</li>
  <li>How do I download an opened PDF? (I tried an action chain, but it doesn't work)</li>
  </ol>
  
  <p>I have <code>Google Chrome Version 59.0.3071.115 (Official Build) (64-bit)</code>, downloaded via the pip installer.</p>