Watir How to get rows with different classes

2019-09-19 07:56发布

问题:

I need to get all rows with class named 'odd_row' or 'even_row'.

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 class="even_row"> … </tr>
    <tr class="subjectField" style="display:none"> … </tr>
</tbody>

I tried this:

@b.table(:class => 'color_table').tbody.trs(:class => ('odd_row' || 'even_row').size

But it returns 1.

Does anybody know how to solve this problem?

回答1:

If you want to do an "or" of classes, you need to use a regular expression. In regular expressions, "or" is done using a single pipe character "|". The class locator you would want is:

:class => /odd_row|even_row/

Therefore, to count all odd and even rows, you want:

@b.table(:class => 'color_table')
  .tbody
  .trs(:class => /odd_row|even_row/)
  .size


回答2:

You are calling .size at the end. This gets the size of the array as an integer. You can try without it.