python?selenium中Excel數(shù)據(jù)維護(hù)指南
接著python里面的xlrd模塊詳解(一)中我們我們來(lái)舉一個(gè)實(shí)例:
我們來(lái)舉一個(gè)從Excel中讀取賬號(hào)和密碼的例子并調(diào)用:
♦1.制作Excel我們要對(duì)以上輸入的用戶(hù)名和密碼進(jìn)行參數(shù)化,使得這些數(shù)據(jù)讀取自Excel文件。我們將Excel文件命名為data.xlsx,其中有兩列數(shù)據(jù),第一列為username,第二列為password。
♦2.讀取Excel代碼如下
#-*- coding:utf-8 -*- import xlrd,time,sys,unittest #導(dǎo)入xlrd等相關(guān)模塊 class Data_Excel(unittest.TestCase):# 封裝在Data_Excel類(lèi)里面方便后面使用 file_addrec = r'C:\Users\liqiang22230\Desktop\date.xlsx' #定義date.xlsx數(shù)據(jù)維護(hù)Excel的路徑文件 def open_excel(self,file = file_addrec):#file = file_addrec #注意在class中def中一定要帶self try:#檢驗(yàn)文件有沒(méi)有被獲取到 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='用戶(hù)表'): #把這個(gè)讀取Excel中封裝在excel_table_byindex函數(shù)中,這時(shí)需要三個(gè)參數(shù)1.文件2.sheet名稱(chēng),列所在的行數(shù) self.data = xlrd.open_workbook(file)#獲取Excel數(shù)據(jù) self.table = self.data.sheet_by_name(by_index)#使用sheet_by_name獲取sheet頁(yè)名叫用戶(hù)表的sheet對(duì)象數(shù)據(jù) self.colnames = self.table.row_values(colnameindex)#獲取行數(shù)下標(biāo)為0也就是第一行Excel中第一行的所有的數(shù)據(jù)值 self.nrows = self.table.nrows #獲得所有的有效行數(shù) list = []#總體思路是把Excel中數(shù)據(jù)以字典的形式存在字符串中一個(gè)字典當(dāng)成一個(gè)列表元素 for rownum in range(1,self.nrows): row = self.table.row_values(rownum)#獲取所有行數(shù)每一行的數(shù)據(jù)值 if row: app = {}#主要以{'name': 'zhangsan', 'password': 12324.0},至于字典中有多少元素主要看有多少列 for i in range(len(self.colnames)): #在這個(gè)Excel中,列所在的行有兩個(gè)數(shù)據(jù),所以沒(méi)循環(huán)一行就以這兩個(gè)數(shù)據(jù)為鍵,行數(shù)的值為鍵的值,保存在一個(gè)字典里 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()
執(zhí)行結(jié)果如下:
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.調(diào)用Excel代碼如下:
def Login(self): listdata = excel_table_byindex("E:\\data.xlsx",0)#傳入兩個(gè)參數(shù)1.文件路徑2.第一行所在下標(biāo) if (len(listdata) <= 0 ):#判斷l(xiāng)ist列表中是否有數(shù)據(jù) assert 0 , u"Excel數(shù)據(jù)異常" for i in range(0 , len(listdata) ):#循環(huán)出list中所有的字典 self.driver = webdriver.Chrome() self.driver.get("http://www.effevo.com") assert "effevo" in self.driver.title #點(diǎn)擊登錄按鈕 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下標(biāo)下標(biāo)為i的字典鍵為username的值 self.driver.find_element_by_id('password').send_keys(listdata[i]['password'])#切出list下標(biāo)下標(biāo)為i的字典鍵為password的值 self.driver.find_element_by_xpath(".//*[@id='content']/div/div[6]/input").click() time.sleep(2) self.driver.close()
總結(jié)
到此這篇關(guān)于python selenium中Excel數(shù)據(jù)維護(hù)的文章就介紹到這了,更多相關(guān)python selenium中Excel維護(hù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python 十六進(jìn)制整數(shù)與ASCii編碼字符串相互轉(zhuǎn)換方法
今天小編就為大家分享一篇Python 十六進(jìn)制整數(shù)與ASCii編碼字符串相互轉(zhuǎn)換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07python實(shí)現(xiàn)pdf轉(zhuǎn)換成word/txt純文本文件
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)pdf轉(zhuǎn)換成word和txt純文本文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06基于Numba提高python運(yùn)行效率過(guò)程解析
這篇文章主要介紹了基于Numba提高python運(yùn)行效率過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03python實(shí)現(xiàn)發(fā)送和獲取手機(jī)短信驗(yàn)證碼
這篇文章主要介紹了python實(shí)現(xiàn)發(fā)送和獲取手機(jī)短信驗(yàn)證碼的相關(guān)資料,講解了python如何解決接口測(cè)試獲取手機(jī)驗(yàn)證碼問(wèn)題,感興趣的小伙伴們可以參考一下2016-01-01