MyGithub:https://github.com/williamzxl 最新代码已经上传到Github,以下版本为stupid版本。

由于在下载过程中需要下载不同文件,所以可以把所有类型放在Values的位置。但是公司要下载的uxz文件实在找不到对应的MIME类型。所以自己写了一个FireFox profile(firefox.exe -p),然后自己让对应的文件自动下载即可。

self.profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/xml,
application/xml')

1.

Firefox 文件下载

对于Firefox,需要我们设置其Profile:

  • browser.download.dir:指定下载路径
  • browser.download.folderList:设置成 2 表示使用自定义下载路径;设置成 0 表示下载到桌面;设置成 1 表示下载到默认路径
  • browser.download.manager.showWhenStarting:在开始下载时是否显示下载管理器
  • browser.helperApps.neverAsk.saveToDisk:对所给出文件类型不再弹出框进行询问

2.实例。

需求:公司里面总是需要在OSS,根据OSS num下载相应的文件。

一共写了三部分:autoDownload.py,getUserInfo.py,userInfo.xlsx

#!/usr/bin/env python3
# -*- coding:utf-8 -*- import xlrd class XlUserInfo(object):
def __init__(self,path=''):
self.path = path
self.xl = xlrd.open_workbook(self.path) def get_sheet_info(self):
all_info = []
info0 = []
info1 = []
for row in range(0,self.sheet.nrows):
info = self.sheet.row_values(row)
info0.append(info[0])
info1.append(info[1])
temp = zip(info0,info1)
all_info.append(dict(temp))
return all_info.pop(0) def get_sheetinfo_by_name(self,name):
self.name = name
self.sheet = self.xl.sheet_by_name(self.name)
return self.get_sheet_info() if __name__ == '__main__':
xl = XlUserInfo('userInfo.xlsx')
userinfo = xl.get_sheetinfo_by_name('userInfo')
webinfo = xl.get_sheetinfo_by_name('WebEle')
print(userinfo)
print(webinfo)

主要用来从userInfo.xlsx中读取用户信息,web的元素。

#!/usr/bin/env python3
# -*- coding:utf-8 -*- from selenium import webdriver
from getUserInfo import XlUserInfo
import threading class AutoDownload(object):
def __init__(self,file_type,args, args2):
self.file_type = file_type
self.args = args
self.args2 = args2 def openBrower(self):
self.profile = webdriver.FirefoxProfile()
self.profile.accept_untrusted_certs = True
if self.args2['downloadpath'] is None:
self.profile.set_preference('browser.download.dir', 'c:\\')
else:
self.profile.set_preference('browser.download.dir', self.args2['downloadpath'])
print(self.args2['downloadpath'])
self.profile.set_preference('browser.download.folderList', 2)
self.profile.set_preference('browser.download.manager.showWhenStarting', False)
if self.file_type == 'xml':
self.profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/xml')
elif self.file_type == 'uxz':
self.profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/xml')
elif self.file_type == 'txt':
self.profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/plain')
else:
self.profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/plain')
#3,6 xml,tml file
# profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/xml')
#2,4 txt,chg file
# profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/plain')
self.driver = webdriver.Firefox(firefox_profile=self.profile)
self.driver.implicitly_wait(30)
return self.driver def openUrl(self):
try:
self.driver.get(self.args2['url'])
self.driver.maximize_window()
except:
print("Failed to get {}".format(self.args2['url']))
return self.driver def login(self):
'''
user_name
pwd_name
logIn_name
'''
self.driver.find_element_by_name(self.args['user_name']).send_keys(self.args2['uname'])
if isinstance(self.args2['pwd'],float):
self.driver.find_element_by_name(self.args['pwd_name']).send_keys(int(self.args2['pwd']))
else:
self.driver.find_element_by_name(self.args['pwd_name']).send_keys(self.args2['pwd'])
self.driver.find_element_by_name(self.args['logIn_name']).click()
self.driver.implicitly_wait(10)
return self.driver def download(self):
self.driver.implicitly_wait(15)
self.driver.find_element_by_link_text(self.args['Search_Forms_text']).click()
self.driver.implicitly_wait(30)
self.driver.find_element_by_id(self.args['OSS_Num_type_id']).send_keys(int(self.args2['OSS_num']))
self.driver.find_element_by_id(self.args['Search_button_id']).click()
self.driver.implicitly_wait(10)
self.driver.find_element_by_link_text(str(int(self.args2['OSS_num']))).click()
self.driver.implicitly_wait(20)
# Attachments_text
self.driver.find_element_by_link_text(self.args['Attachments_text']).click()
self.driver.implicitly_wait(10) if self.file_type == 'xml':
self.driver.find_element_by_xpath('//table[4]//tr[3]/td[1]/a').click()
self.driver.implicitly_wait(30)
self.driver.find_element_by_xpath('//table[4]//tr[6]/td[1]/a').click()
elif self.file_type == 'uxz':
self.driver.find_element_by_xpath('//table[4]//tr[5]/td[1]/a').click()
elif self.file_type == 'txt':
self.driver.find_element_by_xpath('//table[4]//tr[2]/td[1]/a').click()
# driver.find_element_by_xpath('//table[4]//tr[6]/td[1]/a').click()
self.driver.implicitly_wait(30)
self.driver.find_element_by_xpath('//table[4]//tr[4]/td[1]/a').click()
else:
self.driver.quit() def quit(self):
self.driver.quit() def Run(self):
self.openBrower()
self.openUrl()
self.login()
self.download()
self.quit() if __name__ == '__main__':
xl = XlUserInfo('userInfo.xlsx')
userinfo = xl.get_sheetinfo_by_name('userInfo')
webinfo = xl.get_sheetinfo_by_name('WebEle')
print(userinfo)
print(webinfo)
down_txt = AutoDownload('txt',webinfo,userinfo)
down_xml = AutoDownload('xml',webinfo,userinfo) threads = []
t1 = threading.Thread(target=down_txt.Run)
t2 = threading.Thread(target=down_xml.Run)
threads.append(t1)
threads.append(t2) for t in threads:
t.start()
for i in threads:
i.join()

Python selenium 文件自动下载 (自动下载器)的更多相关文章

  1. Python Selenium 文件上传之Autoit

    今天补充一种文件上传的方法 主要是因为工作中使用SendKeys方法不稳定,具体方法见: Python Selenium 文件上传之SendKeys 这种方法直接通过命令行执行脚本时没有问题,可以成功 ...

  2. Python Selenium 文件上传之SendKeys

    昨天写了Web 文件下载的ui自动化,下载之后,今天就要写web 文件上传的功能了. 当然从折腾了俩小时才上传成功.下面写一下自己操作的步骤 首先网上说的有很多方法 如 input 标签的最好做了,直 ...

  3. Python selenium 实现大麦网自动购票过程

    一些无关紧要的哔哔: 大麦网是中国综合类现场娱乐票务营销平台,业务覆盖演唱会. 话剧.音乐剧.体育赛事等领域今天,我们要用代码来实现他的购票过程 开搞! 先来看看完成后的效果是怎么样的 开发环境 版 ...

  4. python+selenium生成测试报告后自动发送邮件

    标签(空格分隔): 自动化测试 运行自动化脚本后,会产生测试报告,而将测试报告自动发送给相关人员,能够让对方及时的了解测试情况,查看测试结果. 整个脚本包括三个部分: 生成测试报告 获取最新的测试报告 ...

  5. python+selenium+webdriver+BeautifulSoup实现自动登录

    from selenium import webdriverimport timefrom bs4 import BeautifulSoupfrom urllib import requestimpo ...

  6. Python selenium Chrome正在受到自动软件的控制 disable-infobars无效 的解决方法

    问题解决 前两天更新了google浏览器版本,今天运行以前的脚本,发现options一个参数的配置不生效了. 运行了几次都发现该参数没有生效,也检查了自己的代码参数,没有写错,于是就有了这一波“网中寻 ...

  7. python+selenium 模拟登陆,自动下单

    目前写的实在太粗糙,留着,以后来写上

  8. Python+Selenium学习笔记19 - 自动发送邮件

    发送简单的邮件 用一个QQ邮箱发送到另一个QQ邮件. 首先设置QQ邮箱,邮箱设置 -> 账号 开启SMTP服务,点击开启按钮,按提示进行操作,需要1毛钱的短信费.开启后如下所示 1 # codi ...

  9. Python+Selenium练习篇之11-浏览器上前进和后退操作

    本文来介绍上如何,利用webdriver中的方法来演示浏览器中地址栏旁边的前进和后退功能. 相关脚本代码如下: # coding=utf-8import timefrom selenium impor ...

随机推荐

  1. window.onerror 应用实例

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp75   window.onerror = function(sMessa ...

  2. Centos6.6下安装Python3.5

    centos6.6自带的Python2.6,如果想要安装新版本的Python例如Python2.7+或者Python3.5,不能够用yum安装,那么只能从源码编译安装. Step 1: 安装依赖库和编 ...

  3. 汇编指令-位置无关码(BL)与绝对位置码(LDR)(2)

    位置无关码,即该段代码无论放在内存的哪个地址,都能正确运行.究其原因,是因为代码里没有使用绝对地址,都是相对地址.  位置相关码,即它的地址与代码处于的位置相关,是绝对地址 BL :带链接分支跳转指令 ...

  4. Java单元测试之覆盖率统计eclemma

    安装 有两种安装方法 下载安装(推荐) 地址: http://sourceforge.net/projects/eclemma/ 将解压后的features和plugins目录下的文件分别拷贝到Ecl ...

  5. 联想G50-70安装SSD及WIN10教程

    借着双11的东风,果断入手SSD120G和4G内存条1枚.经过近一周的安装与试运行,笔者实现了SSD+HDD双硬盘+WIN10系统.目前运行体验非常好,开机时间9秒,软件运行也非常流畅.在折腾的过程中 ...

  6. 201521123042 《Java程序设计》第12周学习总结

    本次作业参考文件 正则表达式参考资料 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String ...

  7. Java课程设计 猜数游戏个人博客

    1.团队课程设计博客链接 http://www.cnblogs.com/tt1104/p/7064349.html 2.个人负责模块或任务说明 1.成绩排行榜算法的设计: 2.排行榜存放到文件中, 3 ...

  8. 201521123016 《Java学习笔记》 第11周学习总结

    1. 本周学习总结 2. 书面作业 本次PTA作业题集多线程 1.互斥访问与同步访问 完成题集4-4(互斥访问)与4-5(同步访问) 1.1 除了使用synchronized修饰方法实现互斥同步访问, ...

  9. 你不知道的JavaScript——类型

    一.ECMAScript语言中所有的值均有一个对应的语言类型.ECMAScript语言类型包括Undefined.Null.Boolean.String.Number和Object. 我们这样来定义类 ...

  10. ionic 打包安卓包

    一.配置环境: 先按照之前的文章,配置好环境需要: 二.安装 1. 这里前提是 需要安装  node (地址: http://nodejs.cn/download/) 命令: node -v   // ...