2013年7月30日星期二

WebTable extension

 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??

1 条评论:

  1. f you don't understand my answer, don't ignore it, ask a question.

    回复删除