【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·单片机·嵌入式硬件·自动化
MediaTea19 分钟前
Python OOP 设计思想 13:封装服务于演化
linux·服务器·前端·数据库·python
love530love19 分钟前
突破 ComfyUI 环境枷锁:RTX 3090 强行开启 comfy-kitchen 官方全后端加速库实战
人工智能·windows·python·cuda·comfyui·triton·comfy-kitchen
wang60212521821 分钟前
流式输出注意点
python·状态模式·fastapi
未定义.22122 分钟前
第3篇:UI自动化核心操作:输入、点击、弹窗、下拉框全场景实战
运维·python·ui·自动化·jenkins·集成测试·pytest
276695829225 分钟前
vercel 安全检测逆向 x-vercel-challenge-solution
开发语言·python·solution·vercel-solution·x-vercel·vercel逆向·ensun
dagouaofei27 分钟前
AI PPT 工具怎么选?5个维度对比6款产品
人工智能·python·powerpoint
深蓝电商API34 分钟前
Scrapy日志系统详解与生产环境配置
爬虫·python·scrapy
Irene.ll35 分钟前
DAY25 异常处理
python
努力学习的小洋39 分钟前
Python训练打卡Day4:缺失值处理
开发语言·python