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 xlrdclass XlUserInfo(object):def __init__(self,path=''):self.path = pathself.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 = nameself.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 threadingclass AutoDownload(object):def __init__(self,file_type,args, args2):self.file_type = file_typeself.args = argsself.args2 = args2def openBrower(self):self.profile = webdriver.FirefoxProfile()self.profile.accept_untrusted_certs = Trueif 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.driverdef openUrl(self):try:self.driver.get(self.args2['url'])self.driver.maximize_window()except:print("Failed to get {}".format(self.args2['url']))return self.driverdef login(self):'''user_namepwd_namelogIn_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.driverdef 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_textself.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()

转载于:https://www.cnblogs.com/william126/p/7495238.html

Python selenium 文件自动下载 (自动下载器)相关推荐

  1. python+selenium 实现 问卷星自动抢讲座

    python + selenium 实现 问卷星自动抢讲座 文章目录 python + selenium 实现 问卷星自动抢讲座 一.代码 二.解析 1.安装selenium库 1)PyCharm 编 ...

  2. python + selenium在亚马逊下载产品主图

    python + selenium 在亚马逊下载产品主图 下载的图片是1000像素的,即卖家上传的图片 import requests import xlwings from selenium imp ...

  3. 利用python+selenium带上cookies自动登录bilibili

    利用python+selenium带上cookies自动登录bilibili 环境 selenium的安装 思路 第一部分:手动登录,保存cookies 第二部分:读取cookies自动登录 环境 环 ...

  4. Python+Selenium自动化测试——126邮箱自动登录脚本(登录首页是二维码,切入账号密码输入框)

    Python+Selenium自动化测试--126邮箱自动登录脚本 版权声明:本文为博主原创文章,未经允许不得转载.https://blog.csdn.net/qiao_wan/article/det ...

  5. Python实现文件上传和下载

    Python实现文件上传和下载 用Python开启web服务,在局域网内实现文件上传和下载功能 #!/usr/bin/env python3"""Simple HTTP ...

  6. python + selenium 实现 问卷星自动抢讲座

    python + selenium 实现 问卷星自动抢讲座,抢不到讲座的同学可以借鉴一下 这是我写的代码 from selenium import webdriver from selenium.we ...

  7. 用Python+Selenium来测试登录自动登录163邮箱

    用Python+Selenium来测试登录自动登录163邮箱 1 准备条件 2 程序说明 3 代码 1 准备条件 ① windows系统(也许你是Linux,Mac,自行选择) ②安装了Anacond ...

  8. python selenium自动化,Firefox自动下载文件以及浏览器相关配置

    from selenium import webdriver from selenium.common.exceptions import WebDriverException from seleni ...

  9. python selenium 下载文件_Python Selenium —— 文件上传、下载,其实很简单

    很多selenium学习者被浏览器弹出的文件上传.下载框折磨的痛不欲生,今天博主就带你们轻松搞定上传和下载问题. 上传 上传弹框 文件上传是所有UI自动化测试都要面对的一个头疼问题,要处理这个问题,我 ...

  10. Python+Selenium,让浏览器自动帮你下文献

    在做学术.搞科研的过程中,我们往往需要针对一个特定的主题下载海量的文献.在把几百篇文献下载到电脑的过程中,假如遇到不够友好的数据库不提供批量下载的功能,怎么办?我恰好遇到了这样的批量下载的科研任务和批 ...

最新文章

  1. Python开发环境配置
  2. fastp: 极速全能的FASTQ文件自动质控+过滤+校正+预处理软件
  3. 一秒搭建gitbook
  4. ALV输出无法指定STATUS
  5. SSH连接服务器报错(WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED)的解决方案
  6. inode与ln命令
  7. Codeforces Round #565 (Div. 3) A. Divide it!
  8. 琥珀项目:较小的,面向生产力的Java语言功能
  9. apache commons lang架包介绍
  10. 直播、线上办公、IoT需求井喷,Wi-Fi 6如何防止网络“塞车”?
  11. 源码安装php时出现configure: error: xml2-config not found. Please check your libxml2 installation...
  12. web_submit_data详解
  13. e-target与e-currentTarget的区别
  14. IT高管发出“暗语邮件”求救(图)
  15. 利用模式进行构建第十一讲——文档版本控制模式
  16. firefox插件开发和调试
  17. qq邮箱发件转发php,phpmailer 利用qq邮箱转发邮件的问题
  18. python 排名函数_python 中rank函数怎样理解?
  19. 外贸沟通谈判中加分项,这些你都知道吗?
  20. 北京理工大学硕士论文latex模板

热门文章

  1. IIS启用GZip压缩
  2. bzoj2038 [2009国家集训队]小Z的袜子(hose)
  3. SET NOCOUNT
  4. 转]分享一个可以下载全球影像的网站(包括历史影像)
  5. 模式窗口(Window.ShowModalDialogs)中提交不弹出新窗口
  6. 119 Python程序中的线程操作-线程同步
  7. 如何使用jquery ,浏览器窗口滚动到一定距离,显示div中的内容
  8. 2017.4.18-morning
  9. mac ssh key 的获取 (转)
  10. Fedora17安装QQ2012手记