0

This code is supposed to download the sample pdf file but it only displays.

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", {
  "download.default_directory": r"/Users/ugur/Downloads/",
  "download.prompt_for_download": True,
  "download.directory_upgrade": False,
  "safebrowsing.enabled": True
})
driver = webdriver.Chrome(executable_path="/Users/ugur/Downloads/chromedriver",chrome_options=options)

driver.get('http://www.africau.edu/images/default/sample.pdf')

This is a demonstration, real website is different and it requires authentication so after running the initial part of the code I manually enter username and password and then run a for.

Uğur Dinç
  • 303
  • 3
  • 16
  • Just checking, is Selenium required here? Can you get away with just using [`requests`](http://docs.python-requests.org/en/master/) to download the pdf? – Bailey Parker Feb 28 '19 at 10:59
  • You are right of course, I should have included it. The website requires authentication so I manually enter username and password first – Uğur Dinç Feb 28 '19 at 11:01
  • `requests` can probably handle that as well. Usually the only reason you need to roll out Selenium is if some part of the process requires javascript. I suspect you should just be able to look at the Network tab in the Dev tools of your browser when logging into the site and make the same (presumably POST) request that it does to log in. Requests can even handle cookies: https://stackoverflow.com/questions/31554771/how-to-use-cookies-in-python-requests – Bailey Parker Feb 28 '19 at 11:03
  • Thanks, I have tried this once more right now; to be honest this looks too complicated to me or maybe the website has a more advanced security measure such that these methods somehow do not work. I used selenium before, downloading was rather straight forward :/ I can't understand why it just doesn't work. – Uğur Dinç Feb 28 '19 at 11:13
  • I don't know if you changed the URL but I can download http://www.africau.edu/images/default/sample.pdf without logging in. – Bailey Parker Feb 28 '19 at 11:16
  • It was a sample. Could you download it with the code above as well ? – Uğur Dinç Feb 28 '19 at 11:17
  • This does https://stackoverflow.com/a/34964610/568785 – Bailey Parker Feb 28 '19 at 11:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/189192/discussion-between-ugur-dinc-and-bailey-parker). – Uğur Dinç Feb 28 '19 at 11:20

1 Answers1

0

On your computer, open Chrome.

Navigate to chrome://settings

Go to advance settings.

Under “Privacy”, click Content settings.

Under “PDF Documents," check the box next to "Download PDF files instead of automatically opening.”

Deepak Garud
  • 977
  • 10
  • 18
  • Thank so much this was the answer I was looking for but is there something similar for images? I actually need to download images, I thought it would be the same. – Uğur Dinç Feb 28 '19 at 11:25
  • This is how you could use requests : https://stackoverflow.com/questions/49530566/download-an-image-using-selenium-webdriver-in-python and this would iterate over img tags in selenium and then download: https://pythonspot.com/selenium-get-images/ – Deepak Garud Feb 28 '19 at 11:34