1. 程式人生 > 其它 >selenium結合xlrd

selenium結合xlrd

我們來舉一個從Excel中讀取賬號和密碼的例子並呼叫:

  ♦1.製作Excel我們要對以上輸入的使用者名稱和密碼進行引數化,使得這些資料讀取自Excel檔案。我們將Excel檔案命名為data.xlsx,其中有兩列資料,第一列為username,第二列為password。

  ♦2.讀取Excel程式碼如下

#-*- coding:utf-8 -*-
import xlrd,time,sys,unittest    #匯入xlrd等相關模組
class Data_Excel(unittest.TestCase):# 封裝在Data_Excel類裡面方便後面使用
    file_addrec = r'C:\Users\liqiang22230\Desktop\date.xlsx' #定義date.xlsx資料維護Excel的路徑檔案
    def open_excel(self,file = file_addrec):#file = file_addrec #注意在class中def中一定要帶self
        try:#檢驗檔案有沒有被獲取到
            self.data =xlrd.open_workbook(file)
            return self.data
        except Exception :
            print(file)
            print('eero')
    def excel_table_byindex(self,file = file_addrec,colnameindex=0,by_index='使用者表'):
        #把這個讀取Excel中封裝在excel_table_byindex函式中,這時需要三個引數1.檔案2.sheet名稱,列所在的行數
          self.data = xlrd.open_workbook(file)#獲取Excel資料
          self.table = self.data.sheet_by_name(by_index)#使用sheet_by_name獲取sheet頁名叫使用者表的sheet物件資料
          self.colnames  = self.table.row_values(colnameindex)#獲取行數下標為0也就是第一行Excel中第一行的所有的資料值
          self.nrows = self.table.nrows #獲得所有的有效行數
          list = []#總體思路是把Excel中資料以字典的形式存在字串中一個字典當成一個列表元素
          for rownum in range(1,self.nrows):
            row = self.table.row_values(rownum)#獲取所有行數每一行的資料值
                if row:
                app = {}#主要以{'name': 'zhangsan', 'password': 12324.0},至於字典中有多少元素主要看有多少列
                     for i in range(len(self.colnames)):
             #在這個Excel中,列所在的行有兩個資料,所以沒迴圈一行就以這兩個資料為鍵,行數的值為鍵的值,儲存在一個字典裡
                          app[self.colnames[i]] = row[i]
                    list.append(app)
        print(list)
        return list
a = Data_Excel()
a.excel_table_byindex()
if __name__=="__main__":
    unittest.main()

執行結果如下:

Testing started at 15:47 ...
[{'name': 'zhangsan', 'password': 12324.0}, {'name': 'zhangsan', 'password': 12324.0}, {'name': 'lisi', 'password': 923848.0}, {'name': 'lisi', 'password': 923848.0}, {'name': 'wangmazi', 'password': 213123.0}, {'name': 'wangmazi', 'password': 213123.0}]

Process finished with exit code 0
Empty test suite.

  ♦3.呼叫Excel程式碼如下:

def Login(self):
        listdata = excel_table_byindex("E:\\data.xlsx",0)#傳入兩個引數1.檔案路徑2.第一行所在下標
        if (len(listdata) <= 0 ):#判斷list列表中是否有資料
                assert 0 , u"Excel資料異常"
        for i in range(0 , len(listdata) ):#迴圈出list中所有的字典
                self.driver = webdriver.Chrome()
                self.driver.get("http://www.effevo.com")
                assert "effevo" in self.driver.title
                #點選登入按鈕
                self.driver.find_element_by_xpath(".//*[@id='home']/div/div[2]/header/nav/div[3]/ul/li[2]/a").click()
                time.sleep(1)

                self.driver.find_element_by_id('passname').send_keys(listdata[i]['username'])#切出list下標下標為i的字典鍵為username的值
                self.driver.find_element_by_id('password').send_keys(listdata[i]['password'])#切出list下標下標為i的字典鍵為password的值
                self.driver.find_element_by_xpath(".//*[@id='content']/div/div[6]/input").click()

                time.sleep(2)
          self.driver.close()