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开始
  • 相关阅读:
    Umbraco建站指南[0]:前言
    项目开发中一些不得其解的问题
    Maven Install 的傻问题
    html5 audio/video 操作
    CentOS7.3安装MySQL5.7
    Maven将独立jar包安装到本地库
    MyBatis 中 foreach 语句处理 List<Integer>类型
    站内信系统的设计思路
    Spring+MyBatis项目开发代码步骤
    webpack 配置eslint-standard
  • 原文地址:https://www.cnblogs.com/Roger1227/p/3219632.html
Copyright © 2011-2022 走看看