WebTable 扩展
1 # coding:utf-8 2 """ 3 页面 table处理 4 """ 5 6 from selenium import webdriver 7 from selenium.webdriver.common.by import By 8 from selenium.common.exceptions import NoSuchElementException 9 10 class WebTable(object): 11 12 def __init__(self, webElement): 13 self.webTable = webElement 14 15 #得到表格中的行数 16 def getRowCount(self): 17 rowConunts = self.webTable.find_elements(By.TAG_NAME, 'tr') 18 return len(rowConunts) 19 20 #得到指定行的列数 21 def getColCount(self, rowIdx): 22 try: 23 rowCounts = self.webTable.find_elements(By.TAG_NAME, 'tr') 24 25 if len(rowCounts) < rowIdx: 26 raise "当前行数大于表格行数" 27 28 #取得当前的 tr 29 rowNum = rowCounts[rowIdx] 30 31 #计算当前行的 td 数 32 colCounts = rowNum.find_elements(By.TAG_NAME, 'td') 33 return len(colCounts) 34 except NoSuchElementException as e: 35 raise NoSuchElementException("Failed to get the cell") 36 37 38 #得到指定单元格内容, 传入指定的行数、列数作为参数 39 def getCellText(self, rowIdx, colIdx): 40 try: 41 rowCounts = self.webTable.find_elements(By.TAG_NAME, 'tr') 42 43 if len(rowCounts) < rowIdx: 44 raise "当前行数大于表格行数" 45 46 #得到对应的行数 47 currentRow = rowCounts[rowIdx] 48 #获取行中所有的td 49 td = currentRow.find_elements(By.TAG_NAME, 'td') 50 51 if len(td) < colIdx: 52 raise "当前列数大于表格列数" 53 54 #取得对应的单元格 55 cell = td[colIdx] 56 return cell.text 57 except NoSuchElementException as e: 58 raise NoSuchElementException("Failed to get the cell") 59 60 if __name__ == '__main__': 61 driver = webdriver.Firefox() 62 driver.get('http://www.w3school.com.cn/tags/tag_table.asp') 63 temp_webTable = WebTable(driver.find_element(By.TAG_NAME, 'table')) 64 print temp_webTable.getRowCount() 65 print temp_webTable.getColCount(3) 66 print temp_webTable.getCellText(3, 2) #行和列的索引从0开始