【问题标题】:Using Watir to click on a specific link when there are other links with the same display text当有其他显示文本相同的链接时,使用 Watir 点击特定链接
【发布时间】:2013-07-10 22:00:41
【问题描述】:

所以我有一个看起来像这样的表格,其中有一个书籍列表,在第一列中,每本书都有两个链接,查看和删除。

我希望能够使用 Watir 找到具有指定书名的行并单击该书的查看按钮。

这是我目前所拥有的

And /^click on View link of "(.*)" on the result set table$/ do |cell_name|
   cellButton("submit.view", cell_name, "")
end
#
#
And /^click on Delete link of "(.*)" on the result set table with no_wait$/ do |cell_name|
   cellButton("submit.delete", cell_name, "no_wait")
end
#
#
def cellButton(submit_type, s_name, s_wait_type)
   c_found = 0
      @browser.tables do |tbl|
         tbl.rows do |r|
            r.each do |c|
               if c.text.to_s.matches(s_name)
                   c_found += 1
                end
               break if c_found > 0
            end
            if c_found > 0
               if s_wait_type == "no_wait"
                  r.button(:name, submit_type).click_no_wait
               else
                  r.button(:name, submit_type).click
               end
            end
            break if c_found > 0
         end
      end
end

这里是特定视图按钮的 html

<tr class="even">
     <td class="actionColumn">
         <span style="margin: 0px; padding: 0px; display: inline;">[</span>
         <input id="013c6e2c8187_885b_1bd1f6fc" name="submit.view" class="action_link" size="" value="View" type="button"  onclick="location.href='book_details.html'">
         <span style="margin: 0px; padding: 0px; display: inline;">]</span><br>                                                 
         <div>
             <span style="margin: 0px; padding: 0px; display: inline;">[</span>
             <input id="013c6e2c8187_1194_75ae28a8" name="submit.delete" class="action_link" size="" value="Delete" type="button">
             <span style="margin: 0px; padding: 0px; display: inline;">]</span>                                                  
         </div>
     </td>
     <td>
        book title
     <td>
     <td>
        more tds
     </td>
 </tr>

脚本运行时没有错误,但是没有按下查看链接。

我正在使用 Watir 4.0、Ruby 1.9.3 和 Cucumber 1.3.3

【问题讨论】:

  • 一些观察:1)我相信click_no_wait方法已被弃用(如果我没记错的话); 2) 按钮上有一个onclick 属性,这意味着您可能必须使用fire_event 方法。
  • 在给出你正在使用的 html 示例时,给出所有相关的 html 是很重要的。给我们“链接”很好,因为我们可以看到它实际上是一个看起来像链接的按钮。但是,我们不知道表格的其余部分是什么样的(即书名与按钮的关系如何)。
  • 我添加了更多 html 代码,我还没有机会查看您的答案。谢谢!

标签: ruby cucumber watir automated-tests


【解决方案1】:

假设你的表格 html 是:

<table>
    <tr>
        <td>
            <input id="013c6e2c8187_885b_1bd1f6fc" name="submit.view" class="action_link" size value="View" type="button" onclick="location.href='book_details.html'"> 
        </td>
        <td>
            book title 1
        </td>
    </tr>
    <tr>
        <td>
            <input id="013c6e2c8122_885b_1bd1f6fc" name="submit.view" class="action_link" size value="View" type="button" onclick="location.href='book_details2.html'"> 
        </td>
        <td>
            book title 2
        </td>
    </tr>
</table>

然后你可以看到书名和查看按钮是由它们的父行(tr)相关的。

通过同级元素访问元素的最简单方法是:

  1. 找到具有唯一属性的元素;在这种情况下,带有书名的 td。
  2. 使用parent方法获取父元素(即tr)。
  3. 获取与父元素相关的所需元素(即行中的第一个查看按钮)。

例如,要获取“书名 2”的查看按钮,您可以:

book_title = 'book title 2'
table = browser.table(:id => 'my_table')
book_row = table.td(:text => book_title).parent
book_row.button(:value => 'View').click

上述解决方案将在任何列中查找书名。如果只希望书名文本在一列中,这很好。如果文本可能在另一列中,您将需要这样做:

book_title = 'book title 2'
table = browser.table(:id => 'my_table')
book_row = table.trs.find{ |tr| tr.td(:index => 1).text == book_title }
book_row.button(:value => 'View').click

【讨论】:

  • 我实际上无法按 id 搜索表,因为类似的表可以在具有不同 id 的不同页面上,但我的脚本仍然需要使用它。但这不是你的错,而是我不清楚。我最后只是把你的代码放在@browser.tables.each 中做|table|环形。并相应地更改您的代码。但令我惊讶的是,您设法在 4 行中完成了我需要的操作……而不是我的许多行。感谢您的帮助!
【解决方案2】:

可能有更清洁的方法,但我会试一试。

假设这个简化的 HTML(即带有链接而不是按钮的 2 列表):

<table id="foo">
  <tr><td><a href="http://www.example.com">View</a></td><td>Title One</td></tr>
  <tr><td><a href="http://www.iana.org/domains/reserved">View</a></td><td>Title Two</td></tr>
  <tr><td><a href="http://www.iana.org/numbers">View</a></td><td>Title Three</td></tr>
</table>

此代码应该通过定位书名来工作:

title = "Title Three"

trs = b.table(:id => "foo").rows
row_index = 0
trs.each do |tr|
  tr.each do |cell|
    if cell.text == title
      b.link(:text => "View", :index => "#{row_index}").click
      # alternately, using the fire_event method
      # b.link(:text => "View", :index => "#{row_index}").fire_event("onclick")
    end    
  end
  row_index += 1  
end

【讨论】:

  • 这比我所拥有的要干净得多,但贾斯汀的回答是代码更少,效率更高。感谢您的贡献。我确实接受了您关于已弃用的 click_no_wait 的其他一些建议
  • @livelaughlove:这就是 SO 的美妙之处。每个人都做出贡献,社区决定什么是最好的。很高兴我能提供一些帮助。
猜你喜欢
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
  • 2022-01-04
  • 1970-01-01
  • 2013-12-21
  • 2013-01-18
  • 1970-01-01
相关资源
最近更新 更多