zoukankan      html  css  js  c++  java
  • 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开始
  • 相关阅读:
    用Python证明“真理再前进一步就是谬误”
    senior developer in Hongkong
    Emacs 维基百科,自由的百科全书
    深入C(和C++)
    图灵等价和图灵完备
    2012年中国大陆和香港的工资收入水平市场调查报告
    图灵完备
    linux中vi进行字符替换_井底之蛙_百度空间
    they're hiring
    Linux下游戏大作赏析(三)
  • 原文地址:https://www.cnblogs.com/Roger1227/p/3219632.html
Copyright © 2011-2022 走看看