title: Download PDF using Selenium in Python + save each PDF with an assigned name
body: <p>The aim is to 1) scrape a bunch of PDFs for a bunch of companies 2) save them with the corresponding company names, all from <a href="https://www1.hkexnews.hk/app/appyearlyindex.html?lang=en&amp;board=mainBoard" rel="nofollow noreferrer">https://www1.hkexnews.hk/app/appyearlyindex.html?lang=en&amp;board=mainBoard</a> .</p>
  <p>My code works for downloading the PDFs, but while the snippet responsible for autodownload is convenient:</p>
  <pre><code>chrome_options = Options()
  chrome_options.add_experimental_option('prefs', {
  &quot;download.default_directory&quot;: &quot;/Users/XXX/Downloads&quot;, #Change default directory for downloads
  &quot;download.prompt_for_download&quot;: False, #To auto download the file
  &quot;download.directory_upgrade&quot;: True,
  &quot;plugins.always_open_pdf_externally&quot;: True #It will not show PDF directly in chrome
  })
  </code></pre>
  <p>I still need to save each PDF using the name of each corresponding company and not just a random PDF file name. The company names can be scraped using:</p>
  <pre><code>all_names = driver.find_elements_by_xpath(&quot;//div[@class='applicant-name']&quot;)
  </code></pre>
  <p>But how do modify the full code below to <strong>include a loop that saves each PDF file with each company name (and not a random filename)</strong>?</p>
  <pre><code>chrome_options = Options()
  chrome_options.add_experimental_option('prefs', {
  &quot;download.default_directory&quot;: &quot;/Users/XXX/Downloads&quot;, #Change default directory for downloads
  &quot;download.prompt_for_download&quot;: False, #To auto download the file
  &quot;download.directory_upgrade&quot;: True,
  &quot;plugins.always_open_pdf_externally&quot;: True #It will not show PDF directly in chrome
  })
  
  year = str(input(&quot;Please enter the year for which you want to download the Application Proofs: &quot;))
  link = &quot;https://www1.hkexnews.hk/app/appyearlyindex.html?lang=en&amp;board=mainBoard&amp;year=&quot; + year
  print(&quot;Now loading: &quot;, link)
  print(&quot;Found the following companies: &quot;)
  
  driver = webdriver.Chrome('/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/chromedriver',options=chrome_options)
  wait = WebDriverWait(driver,10)
  driver.get(link)
  
  all_proofs = driver.find_elements_by_xpath(&quot;//tr[@class='record-ap-phip']//a[contains(.,'Full Version')]&quot;)
  all_names = driver.find_elements_by_xpath(&quot;//div[@class='applicant-name']&quot;)
  
  for i in all_names:
      print('---&gt; ', i.text)
  
  print(&quot;\nTotal number of proofs in year &quot;,year,&quot;: &quot;,len(all_proofs))
  Y = 0
  N = 0
  for proof in all_proofs:
      try:
          proof.click()
          wait.until(EC.presence_of_element_located((By.XPATH, &quot;//*[@id='warning-statement-dialog']//label[@for='warning-statement-accept']&quot;))).click()
          wait.until(EC.presence_of_element_located((By.XPATH, &quot;//*[@id='warning-statement-dialog']//a[contains(@class,'btn-ok')]&quot;))).click()
          Y += 1
      except Exception as exc:
          exception = f'An exception occurred.'
          N += 1
  
  print(&quot;Number of application proofs downloaded: &quot;, Y)
  print(&quot;Number of exceptions: &quot;, N)
  </code></pre>