Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 898
Riley Hun Avatar asked Mar 31 '17 20:03

Riley Hun


3 Answers

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
like image 117
Kumar Avatar answered Oct 03 '22 02:10

Kumar


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()
like image 45
Saravana Avatar answered Oct 03 '22 00:10

Saravana


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)
like image 38
user16072805 Avatar answered Oct 03 '22 00:10

user16072805