一、Selenium怎么读取excel文件
1、安装xlrd模块
2、使用xlrd模块来处理excel文件
3、结合pytest的参数化处理方式来实现ddt
二、示例
新建一个excel文件:test_excel.xlsx
新建一个test_excel.py文件
# -*- coding: utf-8 -*-
# @Time: 2022/5/29 4:58 下午
# @Author: wcystart
# @File: test_excel.py
# @description:
import pytest
import xlrd
def get_data():
filename = "test_excel.xlsx"
wb = xlrd.open_workbook(filename)
sheet = wb.sheet_by_index(0) #索引为0的位置对应excel中的第一行
rows = sheet.nrows
clos = sheet.ncols
my_data = []
for row in range(rows):
for col in range(clos):
cell_data = sheet.cell_value(row, col)
my_data.append(cell_data)
return my_data
@pytest.mark.parametrize('name', get_data())
def test01(name):
print(name)
if __name__ == '__main__':
pytest.main(['-vs', 'test_excel.py'])
输出结果:
test_excel.py::test01[tom] tom
PASSED
test_excel.py::test01[rose] rose
PASSED
test_excel.py::test01[tina] tina
PASSED