Watir Rails - Table 用按钮点击一个靠近特定文本

Watir Rails - Table with Buttons click on one near specific Text

我正在使用 Watir 和 Phantomjs 管理自助门户上的客户设置。

我正在处理一个 table 的单列和多行,其中显示了可以订阅的可用包的列表。在每一行我都可以找到描述和一个按钮。每个按钮都有自己的页面生成的 ID,但所有按钮都有相同的描述。 粘贴格式化后的 HTML 我发现

+-----------------------------------+
|       Available Packages          |
+-----------------------------------+
| Package_1             [subscribe] |
| Package_2             [subscribe] |
| Package_3             [subscribe] |
| Package_4             [subscribe] |
| Package_5             [subscribe] |

我正在尝试单击包含文本但未成功的特定行的按钮。我什至不能 select 该行...我该怎么做?

browser.td(:text => /^Package_2/)

return

 => #<Watir::TableDataCell:0x..fc0259e6592e39a6 located=false selector={:text=>/^Package_2/, :tag_name=>"td"}> 

HTML Table 在加载页面生成。有两个包的示例是:

<table id="j_id174" class="scrollTable4" width="820px">
   <thead class="fixedHeader4 headerFormat4">
      <tr>
         <td style="background:none"></td>
         <td width="100%"></td>
      </tr>
   </thead>

   <tbody class="scrollContent4 bodyFormat4">

      <tr id="showdivj_id1740" style="background-color:#e0e0e0;">
         <td style="background-color:#ffffff;background-image:none;"><a href="javascript:DivStatus( 'mondiv','j_id174',0)" style="text-decoration:none;"><img id="imgj_id1740" border="0" src="../imgs/puce_plus.png" width="13" height="13"></a></td>
         <td>
            <div style="position:relative;"><span class="xdt">PACKAGE_1</span><button type="button" onclick="return _chain('blockpage()','submitForm(\'j_id13\',0,{source:\'j_id174:0:j_id180\'});return false;',this,event,true)" class="xe4 x7i">subscribe</button></div>
         </td>
      </tr>
      <tr id="showdivj_id1741" style="background-color:#e0e0e0;">
         <td style="background-color:#ffffff;background-image:none;"><a href="javascript:DivStatus( 'mondiv','j_id174',1)" style="text-decoration:none;"><img id="imgj_id1741" border="0" src="../imgs/puce_plus.png" width="13" height="13"></a></td>
         <td>
            <div style="position:relative;"><span class="xdt">PACKAGE_2</span><button type="button" onclick="return _chain('blockpage()','submitForm(\'j_id13\',0,{source:\'j_id174:1:j_id180\'});return false;',this,event,true)" class="xe4 x7i">subscribe</button></div>
         </td>
      </tr>

    </tbody>

</table>

非常感谢!

要单击的按钮与要匹配的文本位于同一单元格中。您已经检索到具有匹配文本的 td 元素,因此您只需单击其中的一个按钮:

browser.td(:text => /^Package_2/).button.click