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. Linux的netstat查看端口是否开放见解(0.0.0.0与127.0.0.1的区别)

    linux运维都需要对端口开放查看  netstat 就是对端口信息的查看 # netstat -nltp p 查看端口挂的程序 [root@iz2ze5is23zeo1ipvn65aiz ~]# n ...

  2. java 多线程Callable和Runable执行顺序问题详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt125 毫无疑问 Runnable会进行异步执行,此处不多说,主要说明Call ...

  3. tomcat 和 jboss access log 日志输出详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt179 工作中nginx+jboss/tomcat反向代理集成,想打开后端jb ...

  4. 慎用kill -9,kill -15的作用

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt334 Perl语言专家Randal Schwartz在一篇短文里这样写: n ...

  5. linux模拟实现主机跨路由通信

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...

  6. grep&正则表达式

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...

  7. SGI STL内存配置器存在内存泄漏吗?

    阅读了SGI的源码后对STL很是膜拜,很高质量的源码,从中学到了很多.温故而知新!下文中所有STL如无特殊说明均指SGI版本实现. STL 内存配置器 STL对内存管理最核心部分我觉得是其将C++对象 ...

  8. MPLS VPN随堂笔记3

    跨域 ASBR之间运行MPBGP 1.配置AS内部IGP保证环回口互相可达,同时建立LDP邻居 (优先启用 mpls label rang 16 100)方便查看实验现象 2.配置PE1-PE2 PE ...

  9. 转:【Java集合源码剖析】Vector源码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/35793865   Vector简介 Vector也是基于数组实现的,是一个动态数组,其容量 ...

  10. 【1414软工助教】团队作业10——复审与事后分析(Beta版本) 得分榜

    题目 团队作业10--复审与事后分析(Beta版本) 往期成绩 个人作业1:四则运算控制台 结对项目1:GUI 个人作业2:案例分析 结对项目2:单元测试 团队作业1:团队展示 团队作业2:需求分析& ...