2013-07-10 124 views
0

enter image description here使用的Watir當有與相同的顯示文字等環節的特定鏈接,點擊

,所以我有一個表,看起來像,只要有書,並在第一個列有清單是兩個鏈接,查看和刪除,爲每本書。

我希望能夠使用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 

,這裏是針對特定視圖按鈕

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

有當腳本運行沒有錯誤,但該視圖鏈接的HTML沒有按下。

我使用的Watir 4.0,紅寶石1.9.3和1.3.3黃瓜

+0

一對夫婦的意見:1)我相信'click_no_wait'方法已過時(如果我沒有記錯的話);和2)按鈕上有'onclick'屬性,這意味着你可能不得不使用['fire_event'](http://rubydoc.info/gems/watir-webdriver/Watir/Element#fire_event-instance_method)方法。 – orde

+0

當給出你正在使用的html例子時,給出所有相關的html是很重要的。給我們「鏈接」是很好的,因爲我們可以看到它實際上是一個看起來像鏈接的按鈕。但是,我們不知道表格的其餘部分是什麼樣的(即書名與該按鈕的關係如何)。 –

+0

我添加了更多的html代碼,我還沒有機會看看你的答案呢。謝謝! – livelaughlove

回答

2

假設你的表HTML是:

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

此代碼應針對書名工作:

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

我實際上無法按ID搜索表,因爲類似的表可能位於具有不同ID的不同頁面上,但是我的腳本仍然需要可用於此。但那不是你的錯,因爲不清楚是我的錯。我最終只是把你的代碼放在@ browser.tables.each do | table |中循環。並相應地更改您的代碼。但我很驚訝,你設法做到了我所需要的四行......真是我的多行。謝謝你的幫助! – livelaughlove

1

有可能是一個更清潔的方式,但我給它一個鏡頭。

假設這個簡化的HTML(即2列的錶鏈接,而不是按鈕):

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

這比我所擁有的方式更清潔,但Justin的答案沒有代碼和效率更高。感謝你的貢獻。我確實採取了一些其他建議,不贊成使用click_no_wait – livelaughlove

+0

@livelaughlove:這就是SO的美麗。每個人都有貢獻,社區決定什麼是最好的。很高興我可以得到一些幫助。 – orde

相關問題