0

I'm trying to save what I get after simulating a click button on a link "PDF" on a webpage. When I do that, the PDF is download but I want to save it in a specific file. I read some things using retrieve from urllib library, but I can't get an URL for the PDF. Let me explain :

<a class="at-actionDownloadPdfLink" href="/candidates/downloadSeekerDocument.aspx?sPath=private_0/resumes/4ykqgejxuh95ib6r">PDF</a>

When I submit a click button, I can download easily the PDF but I have a big problem saving it in the right place. The code to activate the click button :

submit3 = driver.find_element_by_id("linkResumeTitle")  
submit3.click()

Thank's

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
Mohamed
  • 21
  • 1
  • 7

1 Answers1

1

If you want to be able to download file automatically to desired folder, you might use Preferences as below:

my_folder = "/I/Want/to/save/file/here"

from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

profile = FirefoxProfile ()
profile.set_preference("browser.download.folderList",2)
profile.set_preference("browser.download.manager.showWhenStarting",False) 
profile.set_preference("browser.download.dir", my_folder)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk",'application/pdf')
driver = webdriver.Firefox(firefox_profile=profile)
driver.get(URL)
submit3 = driver.find_element_by_id("linkResumeTitle")
submit3.click()

or you can get required URL as

link = driver.find_element_by_id("linkResumeTitle").get_attribute('href')

and then try

import urllib
import os
urllib.request.urlretrieve(link, os.path.join(my_folder, "file.pdf"))

to download file

Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Thank you for your answer. Is it the same to use webdriver.Chrome() instead of firefox? – Mohamed Apr 20 '17 at 09:39
  • No. [it looks differently for `Chrome`](http://stackoverflow.com/questions/18026391/setting-chrome-preferences-w-selenium-webdriver-in-python) – Andersson Apr 20 '17 at 09:48
  • I don't understand. I used Chrome to get until "submit3.click". From that point, i can't use the rest of your code ? Well, i tryed and it doesn"t work .. – Mohamed Apr 20 '17 at 09:56
  • if I leave filename empty, does it means it will use file original name? – Mahir Yıldızhan Oct 15 '21 at 12:45