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

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

问题描述

>



有一个表,看起来像那里有一个书的列表,在第一列有两个链接,查看和删除,为每本书。



我想能够使用Watir查找具有指定书名的行,然后点击该书的视图按钮。



这里是我到目前为止

  *)结果集表$ / do | cell_name | 
cellButton(submit.view,cell_name,)
end


和/ ^点击在结果集表上使用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
如果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

 < tr class =even> 
< td class =actionColumn>
< span style =margin:0px; padding:0px; display:inline;> [< / span>
< input id =013c6e2c8187_885b_1bd1f6fcname =submit.viewclass =action_linksize =value =Viewtype =buttononclick =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_75ae28a8name =submit.deleteclass =action_linksize =value =Deletetype =button>
< span style =margin:0px; padding:0px; display:inline;>]< / span>
< / div>
< / td>
< td>
book title
< td>
< td>
更多tds
< / td>
< / tr>

当脚本运行时没有错误,但是没有按下视图链接。 p>

我使用Watir 4.0,Ruby 1.9.3和Cucumber 1.3.3

解决方案

假设您的表格html是:

 < table& 
< tr>
< td>
< input id =013c6e2c8187_885b_1bd1f6fcname =submit.viewclass =action_linksize value =Viewtype =buttononclick =location.href ='book_details.html'&
< / td>
< td>
book title 1
< / td>
< / tr>
< tr>
< td>
< input id =013c6e2c8122_885b_1bd1f6fcname =submit.viewclass =action_linksize value =Viewtype =buttononclick =location.href ='book_details2.html'&
< / td>
< td>
book title 2
< / td>
< / tr>
< / table>

然后您可以看到书名和视图按钮是由他们的父行



通过其兄弟访问元素的最简单方法是:


  1. 查找具有唯一属性的元素;

  2. 使用 parent 方法获取父元素(即tr) li>
  3. 获取与父元素相关的所需元素(即行中的第一个视图按钮)。

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

  book_title = 2'
table = browser.table(:id =>'my_table')
book_row = table.td(:text => book_title).parent
book_row.button =>'View')。单击

上述解决方案将在任何列中查找书名。如果书名标题文本只预期在一列中,这是很好的。如果文本可能在另一列中,您将需要执行以下操作:

  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')。 p $ p>

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