Selenium基础:文件上传下载操作 08
1.Selenium基础:浏览器操作 022.selenium代码3.Selenium基础:对象操作 034.Selenium基础:特殊Dom结构操作 075.Selenium基础:下拉框操作 066.Selenium基础:鼠标操作 057.Selenium基础:键盘操作 048.Selenium基础:SSL证书错误处理 139.Selenium基础:配置chrome浏览器 1210.Selenium基础:其他设置 1111.Selenium基础:时间等待 1012.Selenium基础:cookie javascript调用 屏幕截图 09
13.Selenium基础:文件上传下载操作 08
14.Selenium Grid文件上传
input标签使用send_keys()方法上传,非input标签需要借助工具
1、input标签使用send_keys()方法
#文件上传 #“\\"第一个”\"为转义字符 driver.find_element_by_id('uploadfile').send_keys('D:\\Users\\testFile\\test.text') #driver.quit()
2、使用AutoIt上传
#-*-coding:utf-8-*- from selenium import webdriver from time import sleep import os driver = webdriver.Chrome() driver.maximize_window() driver.get("") driver.implicitly_wait(10) #单击上传的按钮 driver.find_element_by_id('fileupload-btn').click() sleep(2) #使用AutoIt进行文件上传 os.system(r'D:\\Users\\testFile\\UpLoadFile.exe') #driver.quit()
AutoIt下载地址:http://www.autoitscript.com/site/autoit/downloads/
- AutoIt Windows Info:用于定位元素,识别并获取windows上的控件信息
- SciTE Script Editor:编辑脚本,将获取的元素写成AutoIt执行脚本
- Compile Script to.exe:将编辑好的脚本AutoIt转换成可执行(.exe)文件
- Run Script:用于执行AutoIt脚本
3、使用WinSpy上传
下载地址:http://sourceforge.net/projects/winspyex/
文件下载
修改下载路径有两种方式:手动修改和使用webdriver的options设置修改浏览器的下载路径
1、手动修改
以Chrome为例 菜单-单击设置-选择高级-下载内容中修改路径
2、通过options修改
在Chrome文件下载中可通过download.default_directory字段值修改
- download.default_directory:设置下载路径
- download.prompt_for_download:设置为False则在下载时不需要提示
#-*-coding:utf-8-*- from selenium import webdriver import time chrome_options = webdriver.ChromeOptions() prefs = { "download.prompt_for_download": False, #弹窗 “download.default_directory": "C:\\Users\\TynamYang\\Desktop\\", #下载目录 } chrome_options.add_experimental_option("prefs", prefs) driver = webdriver.Chrome(chrome_options=chrome_options) driver.get('') time.sleep(1) driver.find_element_by_id('downloadFile').click() #driver.quit()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构