# coding:utf-8
"""
页面 table处理
""" from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException class WebTable(object): def __init__(self, webElement):
self.webTable = webElement #得到表格中的行数
def getRowCount(self):
rowConunts = self.webTable.find_elements(By.TAG_NAME, 'tr')
return len(rowConunts) #得到指定行的列数
def getColCount(self, rowIdx):
try:
rowCounts = self.webTable.find_elements(By.TAG_NAME, 'tr') if len(rowCounts) < rowIdx:
raise "当前行数大于表格行数" #取得当前的 tr
rowNum = rowCounts[rowIdx] #计算当前行的 td 数
colCounts = rowNum.find_elements(By.TAG_NAME, 'td')
return len(colCounts)
except NoSuchElementException as e:
raise NoSuchElementException("Failed to get the cell") #得到指定单元格内容, 传入指定的行数、列数作为参数
def getCellText(self, rowIdx, colIdx):
try:
rowCounts = self.webTable.find_elements(By.TAG_NAME, 'tr') if len(rowCounts) < rowIdx:
raise "当前行数大于表格行数" #得到对应的行数
currentRow = rowCounts[rowIdx]
#获取行中所有的td
td = currentRow.find_elements(By.TAG_NAME, 'td') if len(td) < colIdx:
raise "当前列数大于表格列数" #取得对应的单元格
cell = td[colIdx]
return cell.text
except NoSuchElementException as e:
raise NoSuchElementException("Failed to get the cell") if __name__ == '__main__':
driver = webdriver.Firefox()
driver.get('http://www.w3school.com.cn/tags/tag_table.asp')
temp_webTable = WebTable(driver.find_element(By.TAG_NAME, 'table'))
print temp_webTable.getRowCount()
print temp_webTable.getColCount(3)
print temp_webTable.getCellText(3, 2) #行和列的索引从0开始

最新文章

  1. scss编译
  2. Mongodb故障转移
  3. 【lattice软核】MICO8流程
  4. windows 7 获取SYSTEM权限
  5. Codeforces 439 A. Devu, the Singer and Churu, the Joker
  6. java操作csv文件之javacsv.jar应用
  7. js 实现二叉树
  8. [LeetCode129]Sum Root to Leaf Numbers
  9. 浏览器缓存(Egret项目实例分析)
  10. Mac OS X下让ruby支持tcl/tk
  11. SQLServer之创建数据库快照
  12. springboot 使用Filter
  13. 聊聊JVM(二)说说GC的一些常见概念
  14. PAT A1033 To Fill or Not to Fill (25 分)——贪心
  15. WAS生成的文件:javacore.***.txt 、heapdump.***.phd、core.***.dmp、Snap.***.trc
  16. kubernetes微服务部署
  17. 【转】 .Net MVC4 上传大文件,并保存表单
  18. 阿里巴巴java手册示例
  19. LINUX CentOS7安装字体库
  20. C/C++-标准输入/输出重定向为文件输入/输出

热门文章

  1. Spring Mvc 笔记二之异常和文件上传
  2. Codevs 1958 刺激
  3. OpenCV(2)-Mat数据结构及访问Mat中像素
  4. c++ 中的8种智能指针[转]
  5. 九度OJ 1525 子串逆序打印 -- 2012年Google校园招聘笔试题目
  6. python3实现的web端json通信协议
  7. h5添加音乐
  8. IE6背景图片闪动问题
  9. JavaScript实现url地址自动检测并添加URL链接示例代码
  10. uploadify插件的使用