Using Watir to click on a specific link when there are other links with the same display text

蹲街弑〆低调 提交于 2019-12-08 12:56:03

问题


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


回答1:


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



回答2:


There's likely a cleaner way, but I'll give it a shot.

Assuming this simplified HTML (i.e. 2-column table with links instead of buttons):

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

This code should work by targeting the book title:

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


来源:https://stackoverflow.com/questions/17581687/using-watir-to-click-on-a-specific-link-when-there-are-other-links-with-the-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!