硒下载文件

foo*_*ill 3 python selenium download

我正在尝试制作一个 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);
Run Code Online (Sandbox Code Playgroud)

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

小智 5

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()
Run Code Online (Sandbox Code Playgroud)

这对我有用。我希望它有帮助。如果有更简单的解决方案,我很想知道。