【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()
相关推荐
小陶来咯5 分钟前
FunctionCall实现与Prompt调优
python·ai·prompt
AI 编程助手GPT21 分钟前
ChatGPT 新手入门与实战操作指南
开发语言·人工智能·git·python·chatgpt
原创小甜甜27 分钟前
OOM 排查复盘:Hutool 序列化 Request 导致 Java Heap Space
java·开发语言·python
gf132111129 分钟前
【精确查找python脚本是否在运行】
linux·前端·python
zhangfeng113331 分钟前
DeepSeek V4 适配华为昇腾950 难度及开源情况
人工智能·pytorch·python·机器学习·华为·开源
MU在掘金9169539 分钟前
给AI Agent做一个代码大脑:我用Tree-sitter+ChromaDB+MCP搭了个代码知识库
git·python
噜噜噜阿鲁~40 分钟前
python学习笔记 | 11.5、面向对象高级编程-使用枚举类
笔记·python·学习
包子BI大数据44 分钟前
2.COZE-RAG知识库搭建
python·ai·aigc
七夜zippoe1 小时前
DolphinDB自定义函数:UDF开发指南
开发语言·python·自定义函数·udf·dolphindb
weixin199701080161 小时前
[特殊字符] 电商库存扣减防超卖:分布式锁的三种实现(附Python源码)
开发语言·分布式·python