(1)安裝xlrd pip3 install xlrd
(2)示例腳本
?import pytest import xlrd def get_data(): filename="F:\\學習\\自動化測試\\selenium自動化測試\\selenium_test\\data\\test.xls" # 讀取工作簿 wb=xlrd.open_workbook(filename) # 讀取第一個sheet頁 sheet=wb.sheet_by_index(0) # 讀取行 rows=sheet.nrows # 讀取列 cols=sheet.ncols lst=[] for row in range(rows): for col in range(cols): #根據行列獲得單元格數據 cell_data=sheet.cell_value(row,col) lst.append(cell_data) return lst @pytest.mark.parametrize('name',get_data()) def test1(name): print(name) if __name__ == '__main__': pytest.main(['-sv','test.xls']) ?
運行結果:
【常見問題】:運行測試腳本報錯誤。
最終發現原因是最近xlrd更新到了2.0.1版本,只支持.xls文件。
【解決方法】:
(1)腳本中使用xls文件
(2)可以安裝舊版xlrd,在cmd中運行:
pip3 uninstall xlrd
pip3 install xlrd==1.2.0
以上來自極客時間課程:selenium自動化測試學習總結
以上就是數據驅動測試DDT之Selenium讀取Excel文件的詳細內容,更多關于DDT驅動測試selenium讀取Excel文件的資料請關注服務器之家其它相關文章!
原文鏈接:https://blog.csdn.net/aovenus/article/details/121040054