Selenium下载文件

时间:2014-06-03 08:48:52

标签: python selenium download qa

我正在尝试制作一个selenium程序来自动下载和上传一些文件。

请注意,我不是为了测试而是为了尝试自动执行某些任务。

所以这是我对firefox个人资料的set_preference

profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', '/home/jj/web')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/json, text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream')
profile.set_preference("browser.helperApps.alwaysAsk.force", False);

然而,我仍然看到下载对话框。请帮帮我!

提前致谢

1 个答案:

答案 0 :(得分:4)

Selenium firefox webdriver运行firefox浏览器GUI。当调用下载时,firefox将显示一个弹出窗口,询问您是要查看文件还是保存文件。据我所知,这是浏览器的属性,无法使用firefox首选项或通过设置firefox配置文件变量来禁用它。我可以避免firefox下载弹出窗口的唯一方法是使用Mechanize和Selenium。我使用Selenium获取下载链接,然后将此链接传递给Mechanize以执行实际下载。 Mechanize与GUI实现无关,因此不会显示用户界面弹出窗口。

此剪辑在Python中,是执行下载操作的类的一部分。

  # These imports are required
  from selenium import webdriver
  import mechanize
  import time


  # Start the firefox browser using Selenium
  self.driver = webdriver.Firefox()

  # Load the download page using its URL.
  self.driver.get(self.dnldPageWithKey)
  time.sleep(3)

  # Find the download link and click it
  elem = self.driver.find_element_by_id("regular")
  dnldlink = elem.get_attribute("href")
  logfile.write("Download Link is: " + dnldlink)
  pos = dnldlink.rfind("/")
  dnldFilename = dnldlink[pos+1:]
  dnldFilename = "/home/<mydir>/Downloads/" + dnldFilename
  logfile.write("Download filename is: " + dnldFilename)

  #### Now Using Mechanize ####
  # Above, Selenium retrieved the download link. Because of Selenium's
  # firefox download issue: it presents a download dialog that requires
  # user input, Mechanize will be used to perform the download.

  # Setup the mechanize browser. The browser does not get displayed.
  # It is managed behind the scenes.
  br = mechanize.Browser()

  # Open the login page, the download requires a login
  resp = br.open(webpage.loginPage)

  # Select the form to use on this page. There is only one, it is the
  # login form.
  br.select_form(nr=0)

  # Fill in the login form fields and submit the form. 
  br.form['login_username'] = theUsername
  br.form['login_password'] = thePassword
  br.submit()

  # The page returned after the submit is a transition page with a link
  # to the welcome page. In a user interactive session the browser would
  # automtically switch us to the welcome page.
  # The first link on the transition page will take us to the welcome page.
  # This step may not be necessary, but it puts us where we should be after
  # logging in.
  br.follow_link(nr=0)

  # Now download the file
  br.retrieve(dnldlink, dnldFilename)

  # After the download, close the Mechanize browser; we are done.
  br.close()

这对我有用。我希望它有所帮助。如果有一个更容易的解决方案,我很想知道它。