title: Python selenium download files
body: <p>I would like to use selenium in Python to automate the downloading process. But the current problem is that I can actually get to the right url (where the pdf file is located) using the xpath, but I cannot download the files because of the OS dial box. I found some solutions suggesting the use of <code>webdriver.FirefoxProfile().set.preference</code>. However, since I need to click on the website several times using selenium to get to the right page, I cannot set the url directly with <code>set.preference</code> at the beginning of the program. Could you help me to integrate the <code>set.preference</code> to my existing program? Thank you very much!!</p>
  
  <p>PS. as you can see the website needs authentication.</p>
  
  <p>Here is my current code:</p>
  
  <pre><code>from selenium import webdriver
  from selenium.webdriver.support.ui import WebDriverWait
  import unittest
  import os
  
  
  
  
  class LoginTest(unittest.TestCase):
   def setUp(self):
      self.driver=webdriver.Firefox()
      self.driver.get("myinitialurl")
  
  def test_Login(self):
      driver=self.driver
  
      emailFieldID="userNameInput"
  
      passFieldID="passwordInput"
      loginButtonID="submitButton"
      BBButton="(//a[contains(@href,'blackboard')])"
      coursebutton="(//a[contains(@href,'Course&amp;id=_4572_1&amp;url')])[1]"
  
      docbutton="(//a[contains(@href,'content_id=_29867_1')])"
      conbutton="(//a[contains(@href,'content_id=_29873_1')])"
      paperbutton="(//a[contains(@href,'/xid-26243_1')])"
  
      emailFieldElement=WebDriverWait(driver,10).until(lambda driver:driver.find_element_by_id(emailFieldID))
  
      passFieldElement=WebDriverWait(driver,10).until(lambda driver:driver.find_element_by_id(passFieldID))
      loginButtonElement=WebDriverWait(driver,10).until(lambda driver:driver.find_element_by_id(loginButtonID))
  
      emailFieldElement.clear()
      emailFieldElement.send_keys("username")
      passFieldElement.clear()    
      passFieldElement.send_keys("password")
      loginButtonElement.click()
      BBElement=WebDriverWait(driver,50).until(lambda driver:driver.find_element_by_xpath(BBButton))
      BBElement.click()
      WebDriverWait(driver, 50).until(lambda driver: len(driver.window_handles) == 2)
  
      window_after = driver.window_handles[1]
      driver.switch_to.window(window_after)
      courseElement=WebDriverWait(driver,50).until(lambda driver:driver.find_element_by_xpath(coursebutton))
      courseElement.click()
  </code></pre>
  
  <p>After that normally I should open a pdf file on the website and a dial box. I would like to download the file.</p>
  
  <p>the code of set.preference that I found is as follows: </p>
  
  <pre><code>fp = webdriver.FirefoxProfile()
  
  fp.set_preference("browser.download.folderList",2)
  fp.set_preference("browser.download.manager.showWhenStarting",False)
  fp.set_preference("browser.download.dir", os.getcwd())
  fp.set_preferenc("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
  
  browser = webdriver.Firefox(firefox_profile=fp)
  browser.get("url")
  browser.find_element_by_partial_link_text("button").click()
  </code></pre>
  
  <p>So my question is how to integrate the second part in the first code so as to download the content triggered by the last click. </p>
  
  <p>Otherwise, do you have other easier solutions??</p>
  
  <p>Thank you very much!</p>