使用的Watir到一个特定的点击链接,当有与同一显示文字等环节 [英] Using Watir to click on a specific link when there are other links with the same display text

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

问题描述

所以我有一个表,看起来像,只要有一个书单,并在第一个栏有两个链接,查看和删除,每本书。

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.

我想能够使用的Watir具有指定书名找到该行并点击那个书视图按钮。

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.

下面是我迄今为止

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

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>

有是当脚本运行没有错误,但该视图链接不是pressed。

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

我使用的Watir 4.0,红宝石1.9.3和1.3.3黄瓜

i am using Watir 4.0, Ruby 1.9.3 and Cucumber 1.3.3

推荐答案

假设你的表HTML是:

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>

这时你可以看到这本书的标题和视图按钮用他们的父行(TR)有关。

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. 找到具有独特的属性的元素;在这种情况下,与书名的TD。

  2. 使用母公司获取父元素(即TR)方法。

  3. 获取相对于父元素所需的元素(行即第一个视图按钮)。

例如,要获取视图按钮'书名2',你可以这样做:

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天全站免登陆