简体   繁体   中英

How can I download something with Selenium and Chrome?

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.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM