【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()
相关推荐
moxiaoran575311 分钟前
Flask学习笔记(一)
后端·python·flask
秋氘渔1 小时前
迭代器和生成器的区别与联系
python·迭代器·生成器·可迭代对象
Gu_shiwww1 小时前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
Dxy12393102162 小时前
python把文件从一个文件复制到另一个文件夹
开发语言·python
sonrisa_3 小时前
collections模块
python
折翼的恶魔3 小时前
数据分析:排序
python·数据分析·pandas
天雪浪子3 小时前
Python入门教程之赋值运算符
开发语言·python
站大爷IP4 小时前
5个技巧写出专业Python代码:从新手到进阶的实用指南
python
hrrrrb4 小时前
【Python】字符串
java·前端·python
大翻哥哥4 小时前
Python 2025:低代码开发与自动化运维的新纪元
运维·python·低代码