How do you automatically download a Pdf with Selenium Webdriver in Python

0 votes
I have a URL that takes me directly to a pdf. I would like to automatically download that file. Below is my sample code.

from selenium import webdriver   
            
chromedriver ="//Server/Apps/chrome_driver/chromedriver.exe"                
download_dir = "C:\\Temp\\Download"
driver = webdriver.Chrome(chromedriver)
driver.implicitly_wait(15)
options = webdriver.chrome.options.Options()
profile = {"plugins.plugins_list": [{"enabled": False, "name": "Chrome PDF Viewer"}], # Disable Chrome's PDF Viewer
               "download.default_directory": download_dir , "download.extensions_to_open": "application/pdf"}
options.add_experimental_option("prefs", profile)

driver.get('https://www.troweprice.com/content/dam/trowecorp/Pdfs/TRPIL%20MiFID%20II%20Execution%20Quality%20Report%202017.pdf  ')
Mar 5, 2020 in Selenium by Deshurn
• 120 points

retagged Mar 5, 2020 by Gitika 27,483 views

1 answer to this question.

+1 vote

Try this code,

from selenium import webdriver

download_dir = "C:\\Temp\\Dowmload"  # 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('//Server/Apps/chrome_driver/chromedriver.exe', chrome_options=options)  # Optional argument, if not specified will search path.

driver.get(`https://www.troweprice.com/content/dam/trowecorp/Pdfs/TRPIL%20MiFID%20II%20Execution%20Quality%20Report%202017.pdf`)

For further understanding, you can refer to the Selenium Course.

answered Mar 5, 2020 by Sirajul
• 59,230 points
still not working

Hey, @Madhav,

Could you please post your error logs? It will be better for us to resolve it ASAP.

Hello. Your code is not working. It does not save the PDF page, but opens it in the browser.

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer

How to handle notifications in Python with Selenium (Chrome WebDriver)

Below will help you: You can disable the ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
14,431 views
0 votes
2 answers

How to open a browser window in full screen using Selenium WebDriver with C#

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
16,040 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,685 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,148 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,226 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,879 views
0 votes
1 answer
+2 votes
1 answer

C Sharp with Selenium -- How to Switch one tab to another tab in Csharp selenium

You could use Session.Driver.WindowHandles which is basically to switch ...READ MORE

answered Dec 23, 2019 in Selenium by Sirajul
• 59,230 points
7,961 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP