Answer a question

I wanted to download files using python but I am not able to do it. I tried searching for the ways to do it but i dint find any relevant resource.

Here is my code:

from selenium import webdriver
driver = webdriver.Chrome('/home/user/Downloads/chromedriver')

#The below link is a pdf file and not an HTML file. I want to download this file directly.

driver.get("https://authlink-files-storage.ams3.digitaloceanspaces.com/authlink/transfered_certificates_related_docs/supporting_docs_17_2020_07_24_06_25_764ffb965d1b4ae287a0d3cc01c8dd03")

Now I want to download this file but i am not able to do it.

Answers

If direct download doesn't work you can always workaround using the printing functionality:

  1. Need to use chrome options --kiosk-printing which will automatically click on print button once print dialog is opened

    options = webdriver.ChromeOptions()

    options.add_argument("--kiosk-printing")

  2. Define chrome preferences as JSON string

prefs = {"savefile.default_directory": "your destination path", "printing.default_destination_selection_rules": {"kind": "local", "idPattern": ".*", "namePattern": "Save as PDF"}}

In above prefs, default directory will be used to save your pdf in required location. second pref will select the "save as pdf" option from print dialog automatically

  1. Add pref as experimental options

    options.add_experimental_option("prefs", prefs)

  2. Define driver using chrome options and prefs

    driver = webdriver.Chrome(chrome_options=options)

  3. Once the pdf is opened in url, you can open print dialog using javascript

    driver.execute_script("window.print()")

Your pdf will be saved in the destination path with the same title

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐