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 in Selenium by Deshurn
• 120 points

retagged Mar 5 by Gitika 5,233 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`)
answered Mar 5 by Sirajul
• 58,020 points

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,890 points
6,418 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 5 in Selenium by Sri
• 2,970 points
9,528 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,510 points
3,574 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31 in Selenium by Sri
• 2,970 points
4,578 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,890 points
2,622 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 2 in Selenium by Sri
• 2,970 points
2,919 views
+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
• 58,020 points
1,478 views