【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()
相关推荐
2601_967979125 分钟前
Python第七十四讲 异常处理完全精通 try-except零基础保姆级教程
python·异常处理·编程教程·错误处理·try-except
wuyk55524 分钟前
Spider-第07讲-JSON接口与正则提取
爬虫·python·json
2601_967987371 小时前
Sqlite参考手册
数据库·python·sqlite·api·教程
用户019027581612 小时前
A 股到价提醒机器人:AlphaFeed + 飞书/钉钉 Webhook
python
特立独行的猫A2 小时前
Ionic + 仓颉语言的 cj-tauri 开发框架实战:开发精美移动端 APP
java·后端·python
高洁012 小时前
数据同化与动态状态估计:让孪生持续贴合现实
人工智能·python·深度学习·transformer·迁移学习
通信仿真爱好者3 小时前
第【139】期--基于深度学习的OFDM信道估计方法研究--python完整代码
python·深度学习·ofdm·信道估计
威哥爱编程3 小时前
模拟面试智能体开发实战 07:答完一题就点评一题,结构化输出给分建议,让每道题都变成复盘
python·agent·harmonyos
cpolar技术支持3 小时前
中文文件名排序跟拼音对不上:os.listdir 返回的是文件系统顺序,三个中文名按字节排结果正好相反
python·文件系统·cpolar·编码·排序
Thanks_ks3 小时前
【第 008 讲】在终端中运行 Python 程序:交互模式与脚本模式 | 绝对路径与相对路径 | 以模块方式运行 | 常见报错的识别与排查
python·异常处理·终端·脚本·命令行·cmd·python 解释器