【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()
相关推荐
学地理的小胖砸8 分钟前
【Python 操作 MySQL 数据库】
数据库·python·mysql
安迪小宝9 分钟前
6 任务路由与负载均衡
运维·python·celery
Blossom.11812 分钟前
使用Python实现简单的人工智能聊天机器人
开发语言·人工智能·python·低代码·数据挖掘·机器人·云计算
lisw0523 分钟前
Python高级进阶:Vim与Vi使用指南
python·vim·excel
ayiya_Oese35 分钟前
[模型部署] 3. 性能优化
人工智能·python·深度学习·神经网络·机器学习·性能优化
SoraLuna37 分钟前
「Mac畅玩AIGC与多模态40」开发篇35 - 用 Python 开发服务对接 SearxNG 与本地知识库
python·macos·aigc
noravinsc1 小时前
redis是内存级缓存吗
后端·python·django
王学政21 小时前
LlamaIndex 第九篇 Indexing索引
人工智能·python
百锦再2 小时前
大数据技术的主要方向及其应用详解
大数据·linux·网络·python·django·pygame
盛夏绽放2 小时前
Python字符串常用方法详解
开发语言·python·c#