Selenium Webdriver: How to Download a PDF File with Python?
Asked Answered
S

6

23

I am using selenium webdriver to automate downloading several PDF files. I get the PDF preview window (see below), and now I would like to download the file. How can I accomplish this using Google Chrome as the browser?

Dialog Box

Singleminded answered 31/3, 2017 at 20:56 Comment(1)
Take a look at this answer... maybe it'll help you.Bandaranaike
P
45

Try this code, it worked for me.

options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
"download.default_directory": "C:/Users/XXXX/Desktop", #Change default directory for downloads
"download.prompt_for_download": False, #To auto download the file
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True #It will not show PDF directly in chrome
})
self.driver = webdriver.Chrome(options=options)
Pumice answered 29/1, 2019 at 18:12 Comment(5)
Thanks! great answer!Muscid
This didn't work for me until I changed the default directory to use backslash, so instead of "C:/Users/XXXX/Desktop" I use "C:\\Users\\XXXX\\Desktop".Atbara
What is download.directory_upgrade for?Witty
Ordinal0 [0x00A75230+1856048] BaseThreadInitThunk [0x76FDFA29+25] RtlGetAppContainerNamedObjectPath [0x77A37B5E+286] RtlGetAppContainerNamedObjectPath [0x77A37B2E+238]Gwendolin
Confirming this works using Splinter (based on Selenium) which doesn't do file downloads.Pianette
S
5

I did it and it worked, don't ask me how :)

options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {
#"download.default_directory": "C:/Users/517/Download", #Change default directory for downloads
#"download.prompt_for_download": False, #To auto download the file
#"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True #It will not show PDF directly in chrome 
})
driver = webdriver.Chrome(options=options)
Storehouse answered 30/5, 2021 at 1:39 Comment(1)
Ordinal0 [0x00A75230+1856048] BaseThreadInitThunk [0x76FDFA29+25] RtlGetAppContainerNamedObjectPath [0x77A37B5E+286] RtlGetAppContainerNamedObjectPath [0x77A37B2E+238]Gwendolin
C
4

I found this piece of code somewhere on Stackoverflow itself and it serves the purpose for me without having to use selenium at all.

import urllib.request

response = urllib.request.urlopen(URL)    
file = open("FILENAME.pdf", 'wb')
file.write(response.read())
file.close()
Cartomancy answered 18/6, 2021 at 2:39 Comment(1)
This method will only work for non-authenticated sessions. It is not robust to websites which require a login. @Kumar's answer will work for both non-authenticated and authenticated sessions.Pianette
O
3

You can download the pdf (Embeded pdf & Normal pdf) from web using selenium.

from selenium import webdriver

download_dir = "C:\\Users\\omprakashpk\\Documents" # for linux/*nix, download_dir="/usr/Public"
options = webdriver.ChromeOptions()

profile = {"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}], # Disable Chrome's PDF Viewer
               "download.default_directory": download_dir , "download.extensions_to_open": "applications/pdf"}
options.add_experimental_option("prefs", profile)
driver = webdriver.Chrome('C:\\chromedriver\\chromedriver_2_32.exe', chrome_options=options)  # Optional argument, if not specified will search path.

driver.get(`pdf_url`)

It will download and save the pdf in directory specified. Change the download_dir location and chrome driver location as per your convenience.

You can download chrome driver from here.

Hope it helps!

Outsole answered 9/2, 2018 at 11:50 Comment(6)
this works with gui, if I add options.add_argument('headless') it doesn't work. Any idea why?Nicodemus
Try add_argument("--headless"). It works with python3. I am sure, it will work for python 2 also.Outsole
I'm also using python3. it might be working for other pdf links but for AWS S3 links, it's not working. eg:http://spark-public.s3.amazonaws.com/nlp/slides/AdvancedMaxent.pdf . Even wget doesn't for aws links. I'm not sure how aws checks you whether you are in gui mode or not.Nicodemus
it seems that 'not allowing' file downloads in headless mode is a security feature bugs.chromium.org/p/chromium/issues/detail?id=696481#c39Nicodemus
@ Om Prakash, have you tested your code with mode of headless chrome? Because I tested the code from your github page in headless chrome and it didn't work.Bendicta
@Om Prakash, If I would like to download an xml?Habilitate
E
-2

In My case it worked without any code modification,Just need to disabled the Chrome pdf viewer

Here are the steps to disable it

  1. Go into Chrome Settings
  2. Scroll to the bottom click on Advanced
  3. Under Privacy And Security - Click on "Site Settings"
  4. Scroll to PDF Documents
  5. Enable "Download PDF files instead of automatically opening them in Chrome"
Enfield answered 20/5, 2020 at 18:18 Comment(0)
O
-2

You can download the PDF file using Python's requests library

import requests
pdf_url = driver.current_url       # Get Current URL
response = requests.get(pdf_url)
file_name = 'filename.pdf'
with open(file_name, 'wb') as f:
   f.write(response.content)
Outdare answered 17/2, 2023 at 7:5 Comment(1)
I believe this answer was downvoted because the question concerns a website with an embedded PDF (i.e. <embed> tag) where this wouldn't work. However, I have a use case where the PDF is being displayed in-brower from the website and this answer is a far better solution than using selenium. I am commenting to note this for any others that see this post.Sustentation

© 2022 - 2024 — McMap. All rights reserved.