python+selenium webdriver 如何处理table

Table对象是自动化测试中经常需要处理的对象。由于webdriver中没有专门的table类,所以我们需要简单的封装出一个易用易扩展的Table类来帮助简化代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module EasyWrap
     class EasyWrapError < StandardError; end
     class NotValidElementError < EasyWrapError; end 
     class IncorrectdIndexError < EasyWrapError; end 
  
     class TableBase
         attr_reader :e
         def initialize e
             raise NotValidElementError unless e.is_a?(Selenium::WebDriver::Element)
             @e = e
         end
  
         def method_missing(m, *params, &blk)
             @e .send(m, *params) if @e .respond_to?(m)
         end
     end #TableBase
     class Table < TableBase
         def initialize e
             super (e)
             __rows
         end
         def __rows
             @rows = @e .find_elements( :css => 'tr' )
         end
         private :__rows
  
         def rows
             all_rows = []  
             @rows . each { |r| rows << TableRow. new (r)}
             all_rows
         end
  
         def row_count
             @rows .size
         end
  
         def [](index)
             valid_index? index
             TableRow. new @rows [index]
         end
  
         def valid_index?(index)
             raise IncorrectdIndexError if index.to_i > row_count
         end
     end #Table
  
     class TableRow < TableBase
         def initialize e
             super (e)
             __cells
         end
  
         def __cells
             @cells = @e .find_elements( :tag_name => 'td' )
             # 如果找不到td那么试着去找th
             @cells = @e .find_elements( :tag_name => 'th' ) if @cells .empty?
         end
         private :__cells
  
         def cells
             all_cells = []
             @cells . each {|c| all_cells << TableCell. new (c)}
         end
  
         def cell_count
             @cells .size
         end
  
         def [](index)
             valid_index? index
             TableCell. new @cells [index]
         end
  
         def valid_index?(index)
             raise IncorrectdIndexError if index.to_i > cell_count
         end
     end #TableRow
  
     class TableCell < TableBase
     end #TableCell
end #EasyWrap

EasyWrap定义了3个类

  • Table类,代表1个table对象,可以通过该对象来访问该table的行和列;

  • TableRow类 代表table的某1行,可以通过其访问table的某一列;

  • TableCell类 代表table的某1个单元格

假如我们需要点击table的第3行第1列,通过EasyWrap,我们可以这样来实现

1
2
talbe = EasyWrap::Table(dr.find_element( :id => 'table_id' ))
table[ 2 ][ 0 ].click

以下面的html代码为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html>
     <head>
         <title>Table</title>
         <style>
             table {border: 1px solid #ccc}
         </style>
     </head>
     <body>
         <table id = "t" >
             <th> C1 </th><th> C2 </th><th> C3 </th>
             <tr>
                 <td>v1</td>
                 <td>v2</td>
                 <td>v3</td>
             </tr>
             <tr>
                 <td>k1</td>
                 <td>k2</td>
                 <td>k3</td>
             </tr>
         </table>
     </body>
</html>

接下来的脚本实现了高亮页面上table元素首行首列的功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
require 'rubygems'
require 'selenium-webdriver'
require './easy_wrap'
include EasyWrap
  
dr = Selenium::WebDriver. for :firefox
table_file = 'file:///' + File .expand_path( File .join( File .dirname( __FILE__ ), 'table.html' ))
dr.get table_file
t = dr.find_element( :id => 't' )
table = Table. new t
p table.row_count
highlight = << JS
     arguments[ 0 ].style.border = "2px solid red"
JS
dr.execute_script highlight, table[ 0 ][ 0 ].e
# 打印第2行第2列的值
puts table[ 1 ][ 1 ].text

转载于:https://www.cnblogs.com/jane0912/p/4171317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值