0

我正在尝试使用 Capybara 在包含特定名称的表中选择一行。不幸的是,Capybara 似乎只在桌子上看到了胡言乱语。这是我从以下代码中得到的:

page.all('table tr').map { |e| e.text }
=> ["", "y y y w y", "y y w y", "y y y w y", "y y w y", "y y w y w y"]

更新:这是它试图处理的表:

<table class="views-table cols-5">
  <thead>
    <tr>
      <th class="views-field views-field-phpcode">Status</th>
      <th class="views-field views-field-name">Name</th>
      <th class="views-field views-field-field-id">ID</th>
      <th class="views-field views-field-phpcode-1">Actions</th>
    </tr>
  </thead>
  <tbody>
    <tr class="odd views-row-first">
      <td class="views-field views-field-phpcode">
        <img title="Complete" src="..."><br>Complete
      </td>
      <td class="views-field views-field-name">Harvey</td>
      <td class="views-field views-field-field-id">1</td>
      <td class="views-field views-field-phpcode-1">
        <a class="existing_submit disabled" href="...">Edit</a>
        <a class="existing_submit confirm-submit disabled" rel="Are you sure you want to reset?" href="...">Reset</a>
        <div style="clear: left;"></div>
        <a class="existing_submit disabled" href="...">Import</a>
        <a class="existing_submit disabled" target="_blank" href="...">View</a>
        <a class="existing_submit" target="_blank" href="...">Replay</a>
        <a class="existing_submit confirm-submit disabled" rel="Are you sure you want to submit the results?" href="...">Submit</a>
        <a class="existing_submit" target="_blank" href="...">Print</a>
      </td>
    </tr>
    <tr class="even">
      <td class="views-field views-field-phpcode">
        <img title="Complete" src="..."><br>Complete
      </td>
      <td class="views-field views-field-name">Lucas</td>
      <td class="views-field views-field-field-id">2</td>
      <td class="views-field views-field-phpcode-1">
        <a class="existing_submit disabled" href="...">Edit School</a>
        <a class="existing_submit confirm-submit disabled" rel="Are you sure you want to reset?" href="...">Reset</a>
        <div style="clear: left;"></div>
        <a class="existing_submit disabled" href="...">Import</a>
        <a class="existing_submit disabled" target="_blank" href="...">View</a>
        <a class="existing_submit" target="_blank" href="...">Replay</a>
        <a class="existing_submit confirm-submit disabled" rel="Are you sure you want to submit the results?" href="...">Submit</a>
        <a class="existing_submit" target="_blank" href="...">Print</a>
      </td>
    </tr>
    <tr class="odd">
      <td class="views-field views-field-phpcode">
        <img title="Complete" src="..."><br>Complete               
      </td>
      <td class="views-field views-field-name">Sarah</td>
      <td class="views-field views-field-field-id">3</td>
      <td class="views-field views-field-phpcode-1">
        <a class="existing_submit disabled" href="...">Edit</a>
        <a class="existing_submit confirm-submit disabled" rel="Are you sure you want to reset?" href="...">Reset</a>
        <div style="clear: left;"></div>
        <a class="existing_submit disabled" href="...">Import</a>
        <a class="existing_submit disabled" target="_blank" href="...">View</a>
        <a class="existing_submit" target="_blank" href="...">Replay</a>
        <a class="existing_submit confirm-submit disabled" rel="Are you sure you want to submit the results?" href="...">Submit</a>
        <a class="existing_submit" target="_blank" href="...">Print</a>
      </td>
    </tr>
    <tr class="even">
      <td class="views-field views-field-phpcode">
        <img title="Complete" src="..."><br>Complete           
      </td>
      <td class="views-field views-field-name">Wanda</td>
      <td class="views-field views-field-field-id">4</td>
      <td class="views-field views-field-phpcode-1">
        <a class="existing_submit disabled" href="...">Edit</a>
        <a class="existing_submit confirm-submit disabled" rel="Are you sure you want to reset?" href="...">Reset</a>
        <div style="clear: left;"></div>
        <a class="existing_submit disabled" href="...">Import</a>
        <a class="existing_submit disabled" target="_blank" href="...">View</a>
        <a class="existing_submit" target="_blank" href="...">Replay</a>
        <a class="existing_submit confirm-submit disabled" rel="Are you sure you want to submit the results?" href="...">Submit</a>
        <a class="existing_submit" target="_blank" href="...">Print</a>
      </td>
    </tr>
    <tr class="odd views-row-last">
      <td class="views-field views-field-phpcode">
        <img title="Ready" src="..."><br>Ready          
      </td>
      <td class="views-field views-field-name">Herman</td>
      <td class="views-field views-field-field-id">5</td>
      <td class="views-field views-field-phpcode-1">
        <a class="existing_submit disabled" href="...">Edit</a>
        <a class="existing_submit confirm-submit" rel="Are you sure you want to reset?" href="...">Reset</a>
        <div style="clear: left;"></div>
        <a class="existing_submit" href="...">Import</a>
        <a class="existing_submit" target="_blank" href="...">View</a>
        <a class="existing_submit" target="_blank" href="...">Run</a>
        <a class="existing_submit confirm-submit disabled" rel="Are you sure you want to submit the results?" href="...">Submit</a>
        <a class="existing_submit disabled" target="_blank" href="...">Print</a>
      </td>
    </tr>
  </tbody>
</table>

Capybara 正在使用 Selenium,因此我可以看到它加载的页面,并且该表格与 Capybara 所看到的完全不同。

有没有人见过这样的事情?任何想法为什么会发生?

4

1 回答 1

0

Capybara和Nokogiri之间似乎存在脱节。使用该native属性可提供正确的值:

page.all('table tr').map { |e| e.native.text }

不幸的是,过滤器也会受到影响,因此以下方法不起作用:

page.should have_selector("table", :text => "Harvey")

于 2013-05-16T15:14:08.047 回答