1.安裝xlrd
xlrd是python用於讀取excel的第三方擴展包,所以在使用xlrd前,需要使用以下命令來安裝xlrd。pip install xlrd
在使用這個命令之前先確定自己有沒有安裝pip模塊
我們需要在C:\Python27\Scripts這個目錄下來執行我們的pip命令
2.使用介紹
- 導入模塊
import xlrd
- 打開excel表
excel=xlrd.open_workbook("excel.xls")
- 獲取表格
#通過索引順序獲取 sytable=excel.sheets()[0] sytable=excel.sheet_by_index(0) #通過工作表名獲取 bmtable=excel.sheet_by_name(u"Sheet1")
注意:通過工作表名獲取的"Sheet1"是填寫你excel中標簽"Sheet1","Sheet2","Sheet3"....
- 獲取行數和列數
#獲取行數 hs=bmtable.nrows #獲取列數 ls=bmtable.ncols
- 獲取整行貨整列的值
#打印第一行的值 rows_values=bmtable.row_values(0) print rows_values #打印第一列的值 cols_values=bmtable.col_values(0) print cols_values
打印結果為:
C:\Python27\python.exe "D:/PyCharm Community Edition 5.0.3/代碼/excel.py" [u'\u82b1\u82b1 ', u'\u82b1\u82b1_\u767e\u5ea6\u641c\u7d22'] [u'bb', u'\u82b1\u82b1_\u767e\u5ea6\u641c\u7d22', u'\u563f\u563f_\u767e\u5ea6\u641c\u7d22', u'\u62c9\u62c9_\u767e\u5ea6\u641c\u7d22']
注意:行號,列號是從索引0開始的
- 循環行列表值
#獲取行數 hs=bmtable.nrows #通過行數值的多少遍歷出表格中的值 for i in range(1,hs): print bmtable.row_values(i)
- 單元格
cell_a1=bmtable.cell(0,0).value print cell_a1 cell_b4=bmtable.cell(3,1).value print cell_b4
打印結果:
C:\Python27\python.exe "D:/PyCharm Community Edition 5.0.3/代碼/excel.py" aa 拉拉_百度搜索
表格信息
注意:索引是從0開始的,cell(3,1)的格式是cell(行row,列col)
總結示例:將表格中的所有的數值打印出來(表格還是上圖的表格)
#coding=utf-8 import xlrd excel=xlrd.open_workbook(u"aa.xlsx") table=excel.sheet_by_index(0) hs=table.nrows for i in range(hs): print table.row_values
打印結果:
3.示例演示
將上表格中的第一列內容到百度去搜索,然后將搜索的標題與表格的一列作比較
1 #coding=utf-8 2 from selenium import webdriver 3 import unittest 4 import HTMLTestRunner 5 import sys 6 from time import sleep 7 import xlrd 8 reload(sys) 9 sys.setdefaultencoding("utf-8") 10 class baidutest: 11 def __init__(self,path): 12 self.path=path 13 def load(self): 14 #打開一個excel文件 15 excel=xlrd.open_workbook(self.path) 16 #獲取一個工作表格 17 table=excel.sheets()[0] 18 #獲取工作表格的行數 19 nrows=table.nrows 20 #循環遍歷數據,將他存到list中去 21 test_data=[] 22 for i in range(1,nrows): 23 print table.row_values(i) 24 test_data.append(table.row_values(i)) 25 #返回數據列表 26 return test_data 27 class baidu(unittest.TestCase): 28 def setUp(self): 29 self.driver=webdriver.Chrome() 30 self.driver.implicitly_wait(30) 31 self.url="http://www.baidu.com" 32 self.path=u"aa.xlsx" 33 34 def test_baidu_search(self): 35 driver=self.driver 36 print u"開始第一個用例百度搜索" 37 #加載測試數據 38 testinfo=baidutest(self.path) 39 data=testinfo.load() 40 print data 41 #循環參數化 42 for d in data: 43 #打開百度首頁 44 driver.get(self.url) 45 #驗證標題 46 self.assertEqual(driver.title,u"百度一下,你就知道") 47 sleep(1) 48 driver.find_element_by_id("kw").clear() 49 #參數化搜索詞 50 driver.find_element_by_id("kw").send_keys(d[0]) 51 sleep(1) 52 driver.find_element_by_id("su").click() 53 sleep(1) 54 print d[0] 55 print driver.title 56 print d[1] 57 #驗證搜索結果的標題 58 self.assertEqual(driver.title,d[1]) 59 sleep(2) 60 61 def tearDown(self): 62 self.driver.quit() 63 64 if __name__=='__main__': 65 test=unittest.TestSuite() 66 test.addTest(baidu("test_baidu_search")) 67 htmlpath=u"D:\\aaaaaaaa.html" 68 fp=file(htmlpath,'wb') 69 runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title=u"baidu測試",description=u"測試用例結果") 70 runner.run(test) 71 fp.close()
打印結果:
練習兔展的登錄(參數化)
表格數據:
代碼實例:
1 #coding=utf-8 2 from selenium import webdriver 3 from selenium.common.exceptions import NoSuchElementException 4 from selenium.webdriver.common.keys import Keys 5 from selenium.webdriver.support.ui import WebDriverWait 6 import time 7 import xlrd 8 def excel(): 9 data=xlrd.open_workbook('login.xlsx') 10 table=data.sheet_by_index(0) 11 nrows=table.nrows 12 print nrows 13 list=[] 14 for i in range(1,nrows): 15 print table.row_values(i) 16 list.append(table.row_values(i)) 17 print list 18 return list 19 def login(): 20 listdata=excel() 21 for d in listdata: 22 driver=webdriver.Chrome() 23 driver.get("http://www.rabbitpre.com/") 24 time.sleep(3) 25 driver.implicitly_wait(30) 26 driver.maximize_window() 27 driver.find_element_by_css_selector("span[class=\"login j-login\"]").click() 28 time.sleep(1) 29 driver.switch_to_frame(1) 30 print u"我要開始登陸咯" 31 time.sleep(3) 32 driver.find_element_by_xpath("//div[@id='LOGREG_1000']//input[@class='user-account']").clear() 33 driver.find_element_by_xpath("//input[@class='user-account']").send_keys(str(d[0])) 34 driver.find_element_by_xpath("//div[@id='LOGREG_1000']//div[contains(@class,'login-container')]//input[@class='user-pass']").clear() 35 driver.find_element_by_xpath("//div[contains(@class,'login-container')]//input[@class='user-pass']").send_keys(str(d[1])) 36 driver.find_element_by_xpath("//div[@id='LOGREG_1000']//div[contains(@class,'login-container')]//button").click() 37 time.sleep(3) 38 try: 39 elem=driver.find_element_by_xpath("//div[@id='DIALOG_1000']//i[@class='icon icon-close']") 40 print u"登陸成功" 41 except NoSuchElementException: 42 assert 0,u"登錄失敗,找不到兔展公告" 43 44 driver.quit() 45 if __name__ == '__main__': 46 login()
運行結果:
需要注意的地方:
- 在參數化表格數據的時候,如果數據是手機號,在運行代碼的時候會自己后面補一位小數(解決方法,在excel中輸入的時候,前面補上一個單引號)
- 在第17行與第18行之間,一定要記住是在for語句循環之后才能return的。不然永遠都只能return第一行的數據