Selenium 文件下载
点击文件下载时,弹出的那个框,webdriver是定位不到的,只有通过第三方工具或方法来操作
一、通过PyUserInput模拟键盘按键下载
PyUserInput是模拟鼠标和键盘的一个模块,替代了python2 中的SendKeys模块。
需要先安装:
pip install PyUserInput
如果用的是python3 32位版本,直接通过pip安装,可以安装成功;
如果用的是puthon3 64位版本,那就比较悲催了,各种报错。
1、查看本机python版本:

2、安装PyUserInput
如果是32位版本的python,那么就直接pip安装吧,下面的安装教程只针对64位版本的python

直接报错:No matching distribution found for pyHook (from PyUserInput)
既然直接用pip安装不了,那我们就通过pyHook来安装
pyHook下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/

根据python版本来下载,如python3.6就下载cp36;python3.7就下载cp37
下载轮子到本地后,直接用pip安装
pip install C:\Users\Administrator\Desktop\pyHook-1.5.1-cp36-cp36m-win_amd64.whl

继续去刚才那个网址下载pyWin32:

下载完后pip安装
pip install C:\Users\Administrator\Desktop\pywin32-223-cp36-cp36m-win_amd64.whl

现在终于安装PyUserInput了
pip install PyUserInput

OK,没报错,安装成功。
试一下:

成功导入。
我们以autoit的下载为例

既然没办法直接定位到这个下载框,我们就模拟键盘发送Tab和Enter键来下载
代码:
# coding = utf-8
from selenium import webdriver
from time import sleep
from pykeyboard import PyKeyboard
# 驱动文件路径
driverfile_path = r'D:\coship\Test_Framework\drivers\IEDriverServer.exe'
# 启动浏览器
driver = webdriver.Ie(executable_path=driverfile_path)
driver.get(r'https://www.autoitscript.com/site/autoit/downloads/')
driver.maximize_window()
driver.implicitly_wait(20)
# 拖动滚动条到页面中部
js = "var q=document.body.scrollTop=55555"
driver.execute_script(js)
sleep(2)
driver.find_element_by_css_selector("img[title='Download AutoIt']").click()
# 建立一个键盘对象
sleep(2)
k = PyKeyboard()
# 按两次Tab键,焦点定位到“运行”按钮上
k.press_key(k.tab_key)
sleep(1)
k.press_key(k.tab_key)
# 按下Enter键,开始下载
sleep(1)
k.press_key(k.enter_key)
# 退出
sleep(5)
driver.quit()
二、通过AutoIt下载
我们还可以通过AutoIt工具(不知道这个工具怎么用的请查看 Selenium 文件上传)



通过AutoIt Windows Info工具获得以下信息:
- 窗口的Title为“文件下载 - 安全警告”,Class为“#32770”“保存”按钮的ClassnameNN为"Button2"
- ”文件名”路径输入框的ClassnameNN为"Edit1"
- 保存窗口的Title为“另存为”,Class为“#32770”
- “保存”按钮的ClassnameNN为"Button1"
根据以上信息编写脚本:
ControlGetFocus("文件下载 - 安全警告")
ControlFocus ( "文件下载 - 安全警告", "", "" )
ControlFocus ( "文件下载 - 安全警告", "保存(&S)", "Button2")
Sleep(2000)
ControlClick ( "文件下载 - 安全警告", "保存(&S)", "Button2","left",1)
WinWaitActive("另存为")
ControlGetFocus("另存为")
ControlFocus ("另存为", "", "" )
;获取下载包的名称
$text=ControlGetText("另存为","","Edit1")
;输入保存路径
ControlSend("另存为","","Edit1","C:\Users\Administrator\Desktop\test\"&$text)
Sleep(2000)
ControlFocus ("另存为", "保存(&S)", "Button1")
ControlClick ("另存为", "保存(&S)", "Button1" )
Sleep(2000)
;当前路径下如果有重名的文件则替换
If WinExists("确认另存为","") Then
WinWaitActive("确认另存为")
ControlGetFocus("确认另存为")
ControlFocus ("确认另存为", "", "" )
ControlFocus ("确认另存为", "是(&Y)", "Button1")
ControlClick ("确认另存为", "是(&Y)", "Button1" )
EndIf
生成exe文件,再用python调用
代码:
# coding = utf-8
from selenium import webdriver
from time import sleep
import os
# 驱动文件路径
driverfile_path = r'D:\coship\Test_Framework\drivers\IEDriverServer.exe'
# 启动浏览器
driver = webdriver.Ie(executable_path=driverfile_path)
driver.get(r'https://www.autoitscript.com/site/autoit/downloads/')
driver.maximize_window()
driver.implicitly_wait(20)
# 拖动滚动条到页面中部
js = "var q=document.body.scrollTop=55555"
driver.execute_script(js)
sleep(2)
driver.find_element_by_css_selector("img[title='Download AutoIt']").click()
# 调用downfile.exe
os.system(r"C:\Users\Administrator\Desktop\downfile.exe")
# 退出
sleep(60)
driver.quit()
三、FireFox浏览器下载
通过FireFox浏览器来下载文件时,可以设置其Profile:
browser.download.dir:指定下载路径browser.download.folderList:设置成0表示下载到桌面;设置成1表示下载到默认路径;设置成2表示使用自定义下载路径;browser.download.manager.showWhenStarting:在开始下载时是否显示下载管理器browser.helperApps.neverAsk.saveToDisk:对所给出文件类型不再弹出框进行询问
代码:
# coding = utf-8
from selenium import webdriver
from time import sleep
# 驱动文件路径
driverfile_path = r'D:\coship\Test_Framework\drivers\geckodriver.exe'
# 设置profile
profile = webdriver.FirefoxProfile()
# 设置下载路径
profile.set_preference("browser.download.dir",r"C:\Users\Administrator\Desktop\test")
# 设置成 2 表示使用自定义下载路径;设置成 0 表示下载到桌面;设置成 1 表示下载到默认路径
profile.set_preference("browser.download.folderList",2)
# 是否显示下载管理器
profile.set_preference("browser.download.manager.showWhenStarting",False)
# 设置成不弹框
profile.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream")
# 启动浏览器
driver = webdriver.Firefox(executable_path=driverfile_path,firefox_profile=profile)
driver.get(r'https://www.autoitscript.com/site/autoit/downloads/')
driver.maximize_window()
driver.implicitly_wait(20)
# 拖动滚动条到显示下载按钮的地方
target = driver.find_element_by_css_selector("img[title='Download AutoIt']")
driver.execute_script("arguments[0].scrollIntoView();", target)
sleep(2)
driver.find_element_by_css_selector("img[title='Download AutoIt']").click()
# 退出
sleep(60)
driver.quit()
Firefox需要针对每种文件类型进行设置,这里需要我们查询对应文件的MIME类型,可以用以下链接进行查询:MIME 参考手册
四、Chrome浏览器下载
通过Chrome浏览器来下载文件时,需设置options:
download.default_directory:设置下载路径profile.default_content_settings.popups:设置为0禁止弹出窗口
代码:
# coding = utf-8
from selenium import webdriver
from time import sleep
# 驱动文件路径
driverfile_path = r'D:\coship\Test_Framework\drivers\chromedriver.exe'
# 设置options
options = webdriver.ChromeOptions()
# 设置下载路径
prefs = {"profile.default_content_settings.popups":0,"download.default_directory":r"C:\Users\Administrator\Desktop\test"}
options.add_experimental_option("prefs",prefs)
# 启动浏览器
driver = webdriver.Chrome(executable_path=driverfile_path,chrome_options=options)
driver.get(r'https://www.autoitscript.com/site/autoit/downloads/')
driver.maximize_window()
driver.implicitly_wait(20)
# 拖动滚动条到显示下载按钮的地方
target = driver.find_element_by_css_selector("img[title='Download AutoIt']")
driver.execute_script("arguments[0].scrollIntoView();", target)
sleep(2)
driver.find_element_by_css_selector("img[title='Download AutoIt']").click()
# 退出
sleep(60)
driver.quit()
Selenium 文件下载的更多相关文章
- Python Selenium 文件下载
Python Selenium 进UI自动化测试时都会遇到文件上传和下载的操作,下面介绍一下文件下载的操作 这里介绍使用FireFox浏览器进行文件下载的操作. 1.设置文件默认下载地址 如下图,fi ...
- selenium WebDriver处理文件下载
下载文件WebDriver 允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中.下面以FireFox 为例执行文件的下载. package com.mypro.jase; ...
- selenium+Python(文件下载)
webdriver允许我们设置默认的文件下载路径,也就是说,文件会自动下载并保存到设置的目录中 下面以Firefox浏览器为例: from selenium import webdriver from ...
- selenium 校验文件下载成功
转自: http://www.seleniumeasy.com/selenium-tutorials/verify-file-after-downloading-using-webdriver-jav ...
- Python+Selenium 自动化实现实例-实现文件下载
#coding=utf-8 from selenium import webdriver #实例化一个火狐配置文件 fp = webdriver.FirefoxProfile() #设置各项参数,参数 ...
- 关于selenium RC的脚本开发
第一.需要录制脚本,找个我也不说了.就是在firefox下下载一个selenium-IDE并且安装. 第二.在工具里找到selenium-IDE点击运行. 第三.默认是红色按钮点击状态的,接下来随便你 ...
- selenium第二课(脚本录制seleniumIDE的使用)
一.Selenium也具有录制功能,可以web中回放,录制的脚本可以转换为java.python.ruby.php等多种脚本语言.seleniumIDE是Firefox的一个插件,依附于Firefox ...
- Python脚本控制的WebDriver 常用操作 <二十七> 文件下载
测试用例场景 webdriver允许我们设置默认的文件下载路径.也就是说文件会自动下载并且存在设置的那个目录中. Python脚本 测试用Python代码: # coding=gbk ''' Crea ...
- Selenium+Python浏览器调用:Firefox
如何查看python selenium的API python -m pydoc -p 4567 说明: python -m pydoc表示打开pydoc模块,pydoc是查看python文档的首选工 ...
随机推荐
- Coins —— POJ-1742
Time limit 3000 ms Memory limit 30000 kB Description People in Silverland use coins.They have coins ...
- TCP/IP 协议是如何保证数据可靠性的?
原文: 网络基础:TCP协议-如何保证传输可靠性 TCP协议传输的特点主要就是面向字节流.传输可靠.面向连接.这篇博客,我们就重点讨论一下TCP协议如何确保传输的可靠性的. 确保传输可靠性的方式TCP ...
- python-redis缓存-pool
#连接池 import redis pool=redis.ConnectionPool(host='192.168.71.140', port=6379) r = redis.Redis(connec ...
- http的导图
- 设置队列中文件上的“X”号的点击事件+uploadLimit动态加1
目的:1.设置文件队列中“x”号的点击事件 2.每次删除服务器文件后,把uploadLimit + 1: 'onUploadSuccess': function (file, data, respon ...
- YOLOv3:Demo needs OpenCV for webcam images
Compiling With CUDA And OpenCV: change the Makefile in the base directory to read: GPU=1 OPENCV=1 ma ...
- GitLab: Deploy keys are not allowed to push code.
被这个问题坑了,大半天.写此博文,愿入坑的童鞋能及时托坑 一.当你第一次pull或者push gitlab远程项目的时候提示你一个该建立一个sshkey,此时你在客户端生成sshkey 二.切记要把这 ...
- CSDN添加文章目录
在发表的文章中,系统根据文章中H1到H6标签自动生成文章目录. 一.发表文章的时候合理使用“标题”标签. 二.目录生成.只要在文章中使用了“标题1”这样的功能,就可以在博文开头看到文章的目录.
- 惟一ID生成方法
几乎所有的业务系统,都存在生成惟一ID的需求,例如: 用户ID:user_id 订单ID: order_id 消息ID: msg_id 常见的ID生成有三大类方法: 一.中间件实现 1.利用Mysql ...
- Swift(三)基本运算符
Swift支持大部分标准C语言的运算符,并且对许多特性进行改进来减少常规编码的错误.除了支持基本运算符外,Swift还提供了2个特殊的运算符,分别是:溢出运算符和区间运算符 首先看下基本运算符 imp ...