当有其他链接具有相同的显示文本时,使用 Watir 点击特定链接 [英] Using Watir to click on a specific link when there are other links with the same display text

查看:25
本文介绍了当有其他链接具有相同的显示文本时,使用 Watir 点击特定链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我目前所拥有的

和/^单击结果集表上(.*)"的查看链接$/do |cell_name|cellButton("submit.view", cell_name, "")结尾##并且/^单击结果集表上(.*)"的删除链接,no_wait$/do |cell_name|cellButton("submit.delete", cell_name, "no_wait")结尾##def cellButton(submit_type, s_name, s_wait_type)c_found = 0@browser.tables 做 |tbl|tbl.rows 做 |r|r.each 做 |c|如果 c.text.to_s.matches(s_name)c_found += 1结尾如果 c_found > 则中断0结尾如果 c_found >0如果 s_wait_type == "no_wait"r.button(:name, submit_type).click_no_wait别的r.button(:name, submit_type).click结尾结尾如果 c_found > 则中断0结尾结尾结尾

这里是特定视图按钮的 html

<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>书名<td><td>更多 tds</td></tr>

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

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

解决方案

假设你的表格html是:

<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>书名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>书名2</td></tr></表>

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

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

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

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

book_title = '书名 2'表 = browser.table(:id => 'my_table')book_row = table.td(:text => book_title).parentbook_row.button(:value => '查看').click

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

book_title = '书名 2'表 = browser.table(:id => 'my_table')book_row = table.trs.find{ |tr|tr.td(:index => 1).text == book_title }book_row.button(:value => '查看').click

so i have a table that looks something like that where there is a list of books and in the very first column there is two links, view and delete, for each book.

i would like to be able to use Watir to find the row with a specified book title and click on that view button for that book.

Here is what I have so far

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

and here is the html for a specific view button

<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>

there is no error when the script is run, however, the view link is not pressed.

i am using Watir 4.0, Ruby 1.9.3 and Cucumber 1.3.3

解决方案

Assume your table html is:

<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>

Then you can see that the book title and view button are related by their parent row (tr).

The easiest way to access an element by its sibling is to:

  1. Find the element with the unique attribute; in this case the td with the book title.
  2. Get the parent element (ie the tr) using the parent method.
  3. Get the desired element in relation to the parent element (ie first view button in the row).

For example, to get the View button for 'book title 2', you can do:

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

The above solution will look for the book title in any column. This is fine if the book title text is only expected to be in the one column. If it is possible that the text will be in another column, you will want to do:

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

这篇关于当有其他链接具有相同的显示文本时,使用 Watir 点击特定链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆