【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()
相关推荐
Tizzy JJ3 小时前
Python + pytest 接口自动化测试框架实战:从零搭建企业级项目骨架
开发语言·python·pytest
海兰3 小时前
【 Python 量化交易】第8章:量化工具箱
开发语言·python
TheBestRucy3 小时前
Python九阳神功之柒:数据分析三剑客·执剑问道
开发语言·python·数据分析
TheBestRucy3 小时前
Python九阳神功之陆:数据库持久化与缓存·乾坤大挪移
数据库·python·缓存
坐吃山猪4 小时前
IDEA调试中Evaluate常用操作
java·python·intellij-idea
Tizzy JJ4 小时前
接口自动化测试实战:如何围绕真实业务场景设计高质量用例
python·自动化·pytest
海兰4 小时前
【 Python 量化交易】第10章:八大经典策略
android·python·kotlin
IT码农-爱吃辣条4 小时前
Milvus 向量数据库 Python 实战教程
数据库·python·milvus
这个人懒得名字都没写4 小时前
OpenCV 官方教程中的测试图片Can’t find required data file
python·opencv