硒下载文件 [英] Selenium download file

查看:114
本文介绍了硒下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请注意,我没有做这个测试,而是试图自动化一些任务。



所以这里是我的firefox配置文件的set_preference

 个人资料.set_preference('browser.download.folderList',2)#自定义位置
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,文本/逗号分隔值,应用程序/八位字节流)
profile.set_preference(browser.helperApps.alwaysAsk.force,False);

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



提前感谢

解决方案

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



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

 #这些导入是必需的
从selenium import webdriver
import mechanize
import time


#使用Selenium启动Firefox浏览器
self.driver = webdriver.Firefox()

#使用其URL加载下载页面。
self.driver.get(self.dnldPageWithKey)
time.sleep(3)

#查找下载链接并单击
elem = self.driver。 find_element_by_id(regular)
dnldlink = elem.get_attribute(href)
logfile.write(下载链接是:+ dnldlink)
pos = dnldlink.rfind(/ )
dnldFilename = dnldlink [pos + 1:]
dnldFilename =/ home /< mydir> / Downloads /+ dnldFilename
logfile.write(下载文件名是:+ dnldFilename )

####现在使用Mechanize ####
#以上,Selenium检索下载链接。因为Selenium的
#firefox下载问题:它提供了一个需要
#用户输入的下载对话框,Mechanize将用于执行下载。

#设置机械化浏览器。浏览器不显示。
#它是幕后管理的。
br = mechanize.Browser()

#打开登录页面,下载需要登录
resp = br.open(webpage.loginPage)

#选择此页面上要使用的表单。只有一个,它是
#登录表单。
br.select_form(nr = 0)

#填写登录表单域并提交表单。
br.form ['login_username'] = theUsername
br.form ['login_password'] = thePassword
br.submit()

#提交是一个转换页面,链接
#到欢迎页面。在用户交互式会话中,浏览器将
#自动切换到欢迎页面。
#转换页面上的第一个链接将带我们到欢迎页面。
#这个步骤可能不是必需的,但它会将我们放在
#登录之后。
br.follow_link(nr = 0)

#现在下载文件
br.retrieve(dnldlink,dnldFilename)

#下载后关闭Mechanize浏览器;我们完了。
br.close()

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


I'm trying to make a selenium program to automatically download and upload some files.

Note that I am not doing this for testing but for trying to automate some tasks.

So here's my set_preference for the firefox profile

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);

Yet, I still see the dialog for download. Please help me!

Thanks in advance

解决方案

The Selenium firefox webdriver runs the firefox browser GUI. When a download is invoked firefox will present a popup asking if you want to view the file or save the file. As far as I can tell this is a property of the browser and there is no way to disable this using the firefox preferences or by setting the firefox profile variables. The only way I could avoid the firefox download popup was to use Mechanize along with Selenium. I used Selenium to obtain the download link and then passed this link to Mechanize to perform the actual download. Mechanize is not associated with a GUI implementation and therefore does not present user interface popups.

This clip is in Python and is part of a class that will perform the download action.

  # 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()

This does work for me. I hope it helps. If there is an easier solution I would love to know it.

这篇关于硒下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆