【selenium】文件上传、下载、读取

    • [1. 文件上传](#1. 文件上传)
    • [2. 文件下载](#2. 文件下载)
    • [3. 文件读取](#3. 文件读取)
      • [3.1 读取txt文件](#3.1 读取txt文件)
      • [3.2 读取xml文件](#3.2 读取xml文件)
      • [3.3 读取csv文件](#3.3 读取csv文件)
      • [3.4 读写excel](#3.4 读写excel)

1. 文件上传

步骤:

  1. 找到文件上传按钮 <input type="file" name ="file"/>
  2. 通过send_keys (文件路径)上传文件

核心代码示例:

python 复制代码
# 定位到文件上传控件
upload_element = driver.find_element_by_id('upload_file')  # 根据实际的元素 ID 进行定位
# 发送文件路径到上传控件
upload_element.send_keys('/path/to/your/file.txt')  # 替换为实际的文件路径

2. 文件下载

  1. 创建Options对象,设置文件下载路径
  2. 点击下载按钮进行文件下载

代码示例:

python 复制代码
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# 设置 Chrome 选项,可以对 Chrome 浏览器的各种行为和设置进行自定义配置。例如:指定下载文件的保存路径、设置浏览器的代理等等
chrome_options = Options()
# 指定下载文件的保存路径
download_dir = chrome_options.add_experimental_option('prefs', {'download.default_directory': '/your/download/directory'})
# 创建 Chrome 驱动实例
driver = webdriver.Chrome(options=chrome_options)

# 打开要下载文件的网页
driver.get('https://example.com/file_to_download')  # 请替换为实际的下载链接
# 找到下载按钮或链接并点击
download_button = driver.find_element_by_id('download_button')  # 根据实际情况修改定位方式
download_button.click()
# 等待文件下载完成
driver.implicitly_wait(10)

# 验证文件是否下载成功
file_path = os.path.join(download_dir, "file.txt")
if os.path.exists(file_path):
    print("文件下载成功!")
else:
    print("文件下载失败!")
# 关闭浏览器
driver.quit()

3. 文件读取

在UI自动化测试过程中,通常会遇到操作步骤相同,但是因为参数不同而预期结果不同的情况,如:

  • admin账号登录显示的是"你好,admin"
  • zhangsan账号登录显示的是"你好,zhangsan"
    为了防止账号信息变更后要去修改代码,通常将这些信息存储在文件中,在登录的时候直接读取文件,将信息作为参数传入login的方法中,即:数据驱动测试,后续会详细讲解什么是数据驱动测试,本次先为数据驱动做好准备工作

3.1 读取txt文件

核心方法:

  • 以只读方式打开文件: open('./info.txt','r')
  • 读取文件中的所有行:file_path.readlines()

示例:

python 复制代码
file_path = open('./info.txt','r')
lines = file_path.readlines()
for line in lines:
    name = line.split(',')[0]
    password = line.split(',')[1]
    print(name,password)

3.2 读取xml文件

xml文件

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <base>
        <login username="admin" passsword="123456"/>
        <login username="guest" passsword="654321"/>
    </base>
    <Province name="广东省">
        <City>广州</City>
        <City>深圳</City>
        <City>珠海</City>
    </Province>
    <Province name="安徽省">
        <City>合肥</City>
        <City>芜湖</City>
        <City>蚌埠</City>
    </Province>
</Root>

读取xml示例

python 复制代码
from xml.dom import minidom

document = minidom.parse("city.xml") # 解析xml
root = document.documentElement # 获得根节点
# 获取所有Province标签
Provinces = root.getElementsByTagName("Province")
# 获取所有City标签
citys = root.getElementsByTagName("City")

# 遍历所有Province标签
for i in range(len(Provinces)):
    print(Provinces[i].getAttribute("name"))

# 遍历所有第一个Province下的子元素文本
children = []
for child in Provinces[0].childNodes:
    # 如果子节点的nodeType等于ELEMENT_NODE,则说明该子节点是一个元素节点
    if child.nodeType == child.ELEMENT_NODE:
        children.append(child.firstChild.data)
print(children)

3.3 读取csv文件

python 复制代码
import csv
data = open('./sample/account.csv', 'r')

for item in data:
    print(item)

3.4 读写excel

关于读写excel 的内容较多,详见:
【使用xlrd、xlutils读写excel】

相关推荐
依旧很淡定2 天前
Selenium(Python)创建Chrome浏览器实例
chrome·python·selenium
小熊出擊2 天前
【pytest】finalizer 执行顺序:FILO 原则
python·测试工具·单元测试·pytest
云闲不收2 天前
接口请求工具对比 apifox apipost swagger postman等
测试工具·postman
sitellla2 天前
Testify Go测试工具包入门教程
git·测试工具·其他·golang
我的xiaodoujiao2 天前
从 0 到 1 搭建 Python 语言 Web UI自动化测试学习系列 9--基础知识 5--常用函数 3
前端·python·测试工具·ui
可可南木3 天前
ICT 数字测试原理 8 - -VCL 的测试参数
开发语言·功能测试·测试工具·pcb工艺
加油20193 天前
爬虫框架: selenium API使用介绍
爬虫·selenium·测试工具
shelter -唯3 天前
基于selenium库的爬虫实战:京东手机数据爬取
爬虫·python·selenium
IDOlaoluo4 天前
Postman-win64-8.6.2-Setup安装教程(附详细步骤,Win64版Postman下载安装指南)
测试工具·postman
运维小菜鸟h4 天前
利用wxpython开发API接口调试工具,类似postman
测试工具·postman