Python selenium 文件自动下载 (自动下载器)
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 文件自动下载 (自动下载器)的更多相关文章
- Python Selenium 文件上传之Autoit
今天补充一种文件上传的方法 主要是因为工作中使用SendKeys方法不稳定,具体方法见: Python Selenium 文件上传之SendKeys 这种方法直接通过命令行执行脚本时没有问题,可以成功 ...
- Python Selenium 文件上传之SendKeys
昨天写了Web 文件下载的ui自动化,下载之后,今天就要写web 文件上传的功能了. 当然从折腾了俩小时才上传成功.下面写一下自己操作的步骤 首先网上说的有很多方法 如 input 标签的最好做了,直 ...
- Python selenium 实现大麦网自动购票过程
一些无关紧要的哔哔: 大麦网是中国综合类现场娱乐票务营销平台,业务覆盖演唱会. 话剧.音乐剧.体育赛事等领域今天,我们要用代码来实现他的购票过程 开搞! 先来看看完成后的效果是怎么样的 开发环境 版 ...
- python+selenium生成测试报告后自动发送邮件
标签(空格分隔): 自动化测试 运行自动化脚本后,会产生测试报告,而将测试报告自动发送给相关人员,能够让对方及时的了解测试情况,查看测试结果. 整个脚本包括三个部分: 生成测试报告 获取最新的测试报告 ...
- python+selenium+webdriver+BeautifulSoup实现自动登录
from selenium import webdriverimport timefrom bs4 import BeautifulSoupfrom urllib import requestimpo ...
- Python selenium Chrome正在受到自动软件的控制 disable-infobars无效 的解决方法
问题解决 前两天更新了google浏览器版本,今天运行以前的脚本,发现options一个参数的配置不生效了. 运行了几次都发现该参数没有生效,也检查了自己的代码参数,没有写错,于是就有了这一波“网中寻 ...
- python+selenium 模拟登陆,自动下单
目前写的实在太粗糙,留着,以后来写上
- Python+Selenium学习笔记19 - 自动发送邮件
发送简单的邮件 用一个QQ邮箱发送到另一个QQ邮件. 首先设置QQ邮箱,邮箱设置 -> 账号 开启SMTP服务,点击开启按钮,按提示进行操作,需要1毛钱的短信费.开启后如下所示 1 # codi ...
- Python+Selenium练习篇之11-浏览器上前进和后退操作
本文来介绍上如何,利用webdriver中的方法来演示浏览器中地址栏旁边的前进和后退功能. 相关脚本代码如下: # coding=utf-8import timefrom selenium impor ...
随机推荐
- 201521123084 《Java程序设计》第13周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. answer: (1)netassist可以用来链接IP端口 (2)accept方法可以用来监听端口,当没 ...
- 事后诸葛亮分析(Beta阶段)
设想和目标 1.我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 解决用户想要随时锻炼四则运算能力的问题:定义的很清楚:有清晰描述. 2.是否有充足的时间来做计划? ...
- Java课程设计——计算器团队博客
1.团队名称.团队成员介绍(需要有照片) 1.1团队名称 707 1.2团队成员介绍 谢元将:组长 罗登宇:组员 王华俊:组员 2. 项目git地址 谢元将 罗登宇 王华俊 3. 项目git提交记录截 ...
- 201521123068《Java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...
- 201521123102 《Java程序设计》第9周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2.书面作业 1.常用异常 题目5-1 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避 ...
- JPA 注解的CascadeType属性
cascade表示级联操作,在表之间的关系映射时用到 CascadeType.MERGE级联更新:若items属性修改了那么order对象保存时同时修改items里的对象.对应EntityManage ...
- spring的一些问题
1.什么是spring? spring是一个轻量级的一站式框架,它的核心有两个部分,1.aop面向切面编程 2.ioc控制反转. 2.什么是aop aop就是面向切面编程,使用aop可以使业务逻辑各个 ...
- (转)Unity3D 之插值计算
在unity3D中经常用线性插值函数Lerp()来在两者之间插值,两者之间可以是两个材质之间.两个向量之间.两个浮点数之间.两个颜色之间,其函数原型如下: Material.Lerp 插值 funct ...
- Nodejs最好的ORM - TypeORM
TypeORM是一个采用TypeScript编写的用于Node.js的优秀ORM框架,支持使用TypeScript或Javascript(ES5, ES6, ES7)开发.目标是保持支持最新的Java ...
- 15 Validation
一.模型选择问题 如何选择? 视觉上 NO 不是所有资料都能可视化;人脑模型复杂度也得算上 通过Ein NO 容易过拟合;泛化能力差 通过Etest NO 能保证好的泛化,不过往往没法提前获得测试资料 ...