【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()
相关推荐
AI视觉网奇4 分钟前
docker v2安装
人工智能·python
Csvn11 分钟前
属性描述符与元类基础
python
小罗和阿泽18 分钟前
GUI 自动化测试 pywinauto测试框架
开发语言·python·功能测试·测试工具·pytest
2301_8073671926 分钟前
Win10开机自启动怎么设置?关闭开机启动6大方法
开发语言·python·pygame
Cocktail_py35 分钟前
Windows直接部署crawlab
windows·python·golang
ZTLJQ40 分钟前
构建现代Web应用:Python全栈框架完全解析
前端·数据库·python
花间相见41 分钟前
【JAVA基础14】—— 二维数组详解:从基础到实战应用
java·python·算法
zzb158042 分钟前
Claude Agent SDK 深度剖析:依赖、权衡与架构选择
人工智能·python·ai
2301_764441331 小时前
使用Python 和 Streamlit 构建的多维度游戏玩家数据分析
python·游戏·数据分析