Python Selenium下载文件

Python Selenium下载文件,python,selenium,download,qa,Python,Selenium,Download,Qa,我正在尝试制作一个selenium程序来自动下载和上传一些文件 请注意,我这样做不是为了测试,而是为了尝试自动化一些任务 下面是我对firefox配置文件的设置偏好 profile.set_preference('browser.download.folderList', 2) # custom location profile.set_preference('browser.download.manager.showWhenStarting', False) profile.set_prefe

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

请注意,我这样做不是为了测试,而是为了尝试自动化一些任务

下面是我对firefox配置文件的设置偏好

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);
然而,我仍然可以看到下载对话框。请帮帮我


提前感谢

Selenium firefox Web驱动程序运行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()
#这些导入是必需的
从selenium导入webdriver
进口机械化
导入时间
#使用Selenium启动firefox浏览器
self.driver=webdriver.Firefox()
#使用其URL加载下载页面。
self.driver.get(self.dnldPageWithKey)
时间。睡眠(3)
#找到下载链接并单击它
elem=self.driver.find\u element\u by\u id(“常规”)
dnldlink=elem.get_属性(“href”)
logfile.write(“下载链接是:”+dnldlink)
pos=dnldlink.rfind(“/”)
dnldFilename=dnldlink[pos+1:]
dnldFilename=“/home//Downloads/”+dnldFilename
logfile.write(“下载文件名为:”+dnldFilename)
####现在使用Mechanize####
#上面,Selenium检索了下载链接。因为硒的
#firefox下载问题:它提供了一个需要
#用户输入,Mechanize将用于执行下载。
#设置mechanize浏览器。浏览器不会显示。
#它是在幕后管理的。
br=mechanize.Browser()
#打开登录页面,下载需要登录
resp=br.open(webpage.loginPage)
#选择要在此页面上使用的表单。只有一个,那就是
#登录表单。
br.选择表格(nr=0)
#填写登录表单字段并提交表单。
br.form['login\u username']=用户名
br.form['login\u password']=密码
br.提交()
#提交后返回的页面是带有链接的转换页面
#转到欢迎页面。在用户交互会话中,浏览器将
#自动将我们切换到欢迎页面。
#过渡页面上的第一个链接将带我们进入欢迎页面。
#这一步可能没有必要,但它使我们达到了我们应该追求的目标
#登录。
br.跟随链接(nr=0)
#现在下载该文件
br.retrieve(dnldlink,dnldFilename)
#下载后,关闭Mechanize浏览器;我们完了。
br.close()

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

使用wget检查文件类型很重要