【Python测试开发】文件上传操作

先写一个上传页面

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
    <link href="http://dcn.bootcss/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
    <div class="row-fluid">
        <div class="span6 well">
            <h3>upload_file</h3>
                <input type="file" name="up_file"/>
        </div>
    </div>
</body>
<script src="http://cdn.bootcss.com.bootstrap/3.3.0/css/bootstrap.min.js"></script>
</html>

实现文件上传操作

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

# 打开浏览器
driver =webdriver.Chrome()
# 最大化
driver.maximize_window()
# 设置隐式等待
driver.implicitly_wait(10)

# 上传文件路径拼接
filepath = "file:///" + os.path.abspath("upload.html")

# 打开上传文件
driver.get(filepath)

# 如果文件上传功能是使用input标签实现的,直接选中该input标签,调用sendkeys上传即可
driver.find_element(By.NAME, "up_file").send_keys(r"C:\Users\Olivia\Pictures\300-300\0.jpg")

time.sleep(5)
driver.quit()
相关推荐
溪语流沙1 小时前
【Python项目实战】虚拟环境与依赖管理:venv / pip / requirements.txt实操
开发语言·python·pip
梯度下降者2 小时前
CukeTest 自动化测试工具2023年度回顾白皮书
自动化测试·python·cuketest·qtquick/qml·linuxatk
circuitsosk2 小时前
从零搭建行业知识平台:向量库+图数据库+传统关系库的多模检索统一层设计
数据库·python·oracle·向量数据库·rag·多模检索
whcyhhh2 小时前
头歌实践教学平台:大数据存储2023(十三3)
大数据·开发语言·python
AR-26710-2 小时前
机器学习复习Day8——异常检测
人工智能·python·机器学习·scikit-learn
千里码aicood2 小时前
Django 基于Django的电脑推荐与分析可视化系统设计与实现
python·django·电脑
未若君雅裁2 小时前
Agent 的结构化输出,ProviderStrategy、ToolStrategy 与错误重试
python·langchain
洛兮银儿2 小时前
pycharm选中同一个词的标记颜色的修改
ide·python·pycharm
郝学胜_神的一滴3 小时前
Effective Python 条款 6:善用拆包告别恼人的下标索引
python·pycharm
汤姆yu3 小时前
基于Django的学习资料共享与智能推荐系统
后端·python·django·毕业设计·学习资料共享