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 ...
随机推荐
- DES、3DES、AES加密方式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt165 DES 支持8位加密解密,3Des支持24位,Aes支持32位.3De ...
- PHP初入--添加内容到框框里并删除
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 关于selenium IDE找不到元素
selenium IDE ,明明存在元素,却找不到元素 ,报错Element not found 标签: seleniumselenium IDE自动化测试ide 2016-10-31 13:25 1 ...
- javascript 学习笔记 -- js获取本地文件信息
JavaScript是跑在浏览器中,所以对于JavaScript读取本地文件不想c++ 和 java那样easy.网上有很多关于读取本地文件的方法,许多是用ActiveXObject控件.Active ...
- 201521123023《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4 ...
- 201521123078《Java程序设计》第七周学习总结
1. 本周学习总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 public boolean contains(Object o) { re ...
- 201521123035《Java程序设计》第三周学习总结
本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 书面作业 1.代码 ...
- 201521123064 《Java程序设计》第13周学习总结
本次作业参考文件 正则表达式参考资料 1. 本章学习总结 1.1 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. ① InetAddress(IP地址对应的类) InetAd ...
- Java课程设计—学生成绩管理系统(201521123002 林楚虹)
1. 团队课程设计博客链接 团队博客链接 2.个人责模块或任务说明 根据学生学号查找学生成绩 根据学生姓名(支持模糊匹配)查找学生成绩 用POI技术导出Excel文件 3.自己的代码提交记录截图 4. ...
- 201521123109 《java程序设计》第14周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...