【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()
相关推荐
Jonathan Star31 分钟前
Ant Design (antd) Form 组件中必填项的星号(*)从标签左侧移到右侧
人工智能·python·tensorflow
努力努力再努力wz1 小时前
【Linux网络系列】:TCP 的秩序与策略:揭秘传输层如何从不可靠的网络中构建绝对可靠的通信信道
java·linux·开发语言·数据结构·c++·python·算法
deep_drink1 小时前
【论文精读(三)】PointMLP:大道至简,无需卷积与注意力的纯MLP点云网络 (ICLR 2022)
人工智能·pytorch·python·深度学习·3d·point cloud
njsgcs2 小时前
langchain+vlm示例
windows·python·langchain
勇气要爆发2 小时前
LangGraph 实战:10分钟打造带“人工审批”的智能体流水线 (Python + LangChain)
开发语言·python·langchain
jz_ddk2 小时前
[实战] 从冲击响应函数计算 FIR 系数
python·fpga开发·信号处理·fir·根升余弦·信号成形
醒醒该学习了!2 小时前
如何将json文件转成csv文件(python代码实操)
服务器·python·json
忘忧记3 小时前
pythonQT版本的图书管理系统
python·fastapi
一只理智恩3 小时前
AI 实战应用:从“搜索式问答“到“理解式助教“
人工智能·python·语言模型·golang
Katecat996633 小时前
输液泵设备检测与识别基于改进YOLO11模型的实现详解_ETB
python