Selenium教程08:文件的上传+下载的示例练习

1.上传李白.txt文件,这里使用的send_keys方法操作,而不是click点击操作,因为使用点击操作之后,Selenium中没有方法对.exe程序操作,它只能对web网页自动化操作。

python 复制代码
# @Author : 小红牛
# 微信公众号:WdPython
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://sahitest.com/demo/php/fileUpload.htm')
Upload_element = driver.find_element(By.ID, 'file')
# 1.上传李白.txt文件的路径
Upload_element.send_keys(r'D:/Wdpython/爬虫/李白.txt')
sleep(2)

# 2.查看上传文件的详情
driver.find_element(By.NAME, 'submit').click()
sleep(5)
driver.quit()

2.Chrome浏览器文件下载示例:设置其options中的download.default_directory表示设置文件的下载路径。

python 复制代码
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
# prefs指定下载文件的存放路径
prefs = {'download.default_directory': 'D:\Wdpython\爬虫\pic'}
options.add_experimental_option('prefs', prefs)
# 1.添加options路径配置
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get('https://sahitest.com/demo/saveAs.htm')
# 2.点击下载
driver.find_element(By.LINK_TEXT, 'testsaveas.zip').click()
sleep(5)
driver.quit()

完毕!!感谢您的收看

----------★★历史博文集合★★----------
我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具

相关推荐
cup111 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi003 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵5 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf5 小时前
Agent 流程编排
后端·python·agent
copyer_xyf6 小时前
Agent RAG
后端·python·agent
copyer_xyf6 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf6 小时前
Agent 记忆管理
后端·python·agent
星云穿梭21 小时前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵1 天前
用 Pygame 实现 15 puzzle
python·数学·游戏