How to download a pdf in selenium python?

Question:

I am using selenium webdriver to automate download PDF files, but instead I get the preview of that pdf file in chrome viewer for pdf file. How can download the file?

enter image description here

Asked By: TheWorrier

||

Answers:

In chrome the pdf file open internally in chrome viewer, Now to download a pdf file in selenium chrome driver python, you required to open pdf externally. To do this turn this option to true. The code is given below.

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)
Answered By: Harsh Narwariya
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.