#encoding=utf-8 class Table(object): # 定義一個私有屬性__table,用於存放table對象 __table = '' def __init__(self, table): # Table類的構造方法 self.setTable(table) def setTable(self, table): # 對私有屬性__table進行賦值操作 self.__table = table def getTable(self): # 獲取私有屬性__table的值 return self.__table def getRowCount(self): # 返回table對象中所有的行tr標簽元素對象 return len(self.__table.find_elements_by_tag_name("tr")) def getColumnCount(self): # 獲取表格對象中的列數 return len(self.__table.find_elements_by_tag_name("tr")[0].\ find_elements_by_tag_name("td")) def getCell(self, rowNo, colNo): # 獲取表格中某行某列的單元格對象 try: # 找到表格中的某一行,因為行號從 0 開始, # 例如要找第三行,則需要進行 3 - 1 = 2來獲取第三行tr元素對象 currentRow = self.__table.find_elements_by_tag_name("tr")[rowNo - 1] # 在找到的某行基礎上,再找這行中的某一列,列號也從 0 開始 currentCol = currentRow.find_elements_by_tag_name("td")[colNo - 1] # 返回找到的單元格對象 return currentCol except Exception as e: raise e def getWebElementInCell(self, rowNo, colNo, by, value): # 獲取表格中某行某列的單元格中某個頁面元素對象, # by表示定位頁面元素的方法,比如id, # value表定位表達式,比如query try: currentRow = self.__table.find_elements_by_tag_name("tr")[rowNo - 1] currentCol = currentRow.find_elements_by_tag_name("td")[colNo - 1] # 獲取具體某個單元格中的某個頁面元素 element = currentCol.find_element(by = by, value = value) # 返回找到的頁面元素對象 return element except Exception as e: raise e