如何在Watir中使用几个条件查找元素?

时间:2014-01-23 10:39:11

标签: ruby watir watir-webdriver

有一个表有哪些行具有不同的类名:first_row,odd_row,even_row和subjectField。

HTML:

<table class="color_table">
    <thead></thead>
    <tbody>
        <tr class="first_row"></tr>
          <td colspan="1" rowspan="1"></td>
          <td colspan="1" rowspan="1"></td>
          <td colspan="1" rowspan="1"></td>
          <td colspan="1" rowspan="1">
            **63**
          </td>
        <tr class="subjectField" style="display:none"></tr>
        <tr class="odd_row"></tr>
        <tr class="subjectField" style="display:none"></tr>
        <tr class="even_row"></tr>
        <tr class="subjectField" style="display:none"></tr>
    </tbody>

其他HTML:

<tbody>
<tr class="first_row"></tr>
<tr class="subjectField" style="display:none"></tr>
<tr class="odd_row"></tr>
<tr class="subjectField" style="display:none"></tr>
<tr>
    <td class="separator" rowspan="1" colspan="10"></td>
</tr>
<tr class="even_row"></tr>
<tr class="subjectField" style="display:none"></tr>

</tbody>

我需要从除了类名为'subjectField'

的行之外的所有行获取信息

我的代码:

table = @f.div(:id => 'household').table(:class => 'color_table')
table.tbody.trs(:class => 'first_row', :class => 'odd_row', :class =>'even_row').each do
    age = tr.td(:index => 3).text
puts age
end

此代码也接受所有行,subjectFields行。 有人知道如何使用我只需要的行吗?

4 个答案:

答案 0 :(得分:1)

要查找除类之外的所有内容,请使用带有否定前瞻的正则表达式:

table.trs(:class => /^(?!subjectField)/).size

如果您想获取每一行的文字:

puts table.trs(:class => /^(?!subjectField)/).collect(&:text)

如果您想获取每个单元格的第四列文本:

puts table.trs(:class => /^(?!subjectField)/).collect do |row|
   row.td(:index => 3).text
end

答案 1 :(得分:0)

这很简单:

table = @f.div(:id => 'household').table(:class => 'color_table')

table_element.count #it will display the count of all rows corresponding to specified table.

table_elements(:class => 'first row').index #will return the array count [0]
table_elements(:class => 'even_row').index #will return the array count [2]

答案 2 :(得分:0)

实际上不是问题

table_elements(:class => 'first row').text # if you need to take the text from the row with corresponding class

table_elements[0].text

答案 3 :(得分:-1)

你有没有试过这样的东西

...(:xpath, "//table/tbody/tr[@class !='subjectField']")...