2017-07-30 54 views
0

我有一個HTML文件中的代碼(如下所示)HTML表中的值:紅寶石獲得從具有相同的元素

<table id="plans" class="brand-table"> 
    <thead> 
     <tr> 
      <th class="domain">Plans</th> 
      <th class="basic">Basic</th> 
      <th class="plus">Plus</th> 
      <th class="prime">Prime</th> 
     </tr> 
    </thead> 

    <tbody>    
     <tr class="even"> 
     <td> 
      www.test.com 
     </td> 
     <td> 
      <input name="upgrade" type="radio"> 
      //this span element is hidden 
      <span class="plan_status"></span> 
     </td> 
     <td> 
      <input name="upgrade" value="plus www.test.com" type="radio"> 
      //this span element is hidden 
      <span class="plan_status"></span> 
     </td> 
     <td> 
      <input name="upgrade" value="prime www.test.com" checked="" type="radio"> 
      <span class="plan_status">current</span> 
     </td> 
     </tr> 
    </tbody> 
</table> 

我要檢查其計劃是通過紅寶石的Watir頁面的當前計劃。下面是腳本:

require 'watir' 

browser = Watir::Browser.new(:chrome) 

browser.goto('file:///C:/Users/Ashwin/Desktop/new.html') 

browser.table(:id, 'plans').tds.each do |table_row| 
    if table_row.input(:value, 'plus www.test.com').text =~ /current/i 
     p 'current plan status is plus' 
    elsif table_row.input(:value, 'prime www.test.com').text =~ /current/i 
     p 'current plan status is prime' 
    else 
     p 'current plan status is basic' 
    end 
end 

但我得到的輸出:

C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.11/lib/watir-webdriver/elements/element.rb:513:in `assert_exists': unable to locate element, using {:value=>"plus www.test.com", :tag_name=>"input"} (Watir::Exception::UnknownObjectException) 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.11/lib/watir-webdriver/elements/element.rb:86:in `text' 
     from C:/Users/Name/Documents/NetBeansProjects/RubyApplication6/lib/new_main15.rb:8:in `block in <main>' 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.11/lib/watir-webdriver/element_collection.rb:29:in `each' 
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.11/lib/watir-webdriver/element_collection.rb:29:in `each' 
     from C:/Users/Name/Documents/NetBeansProjects/RubyApplication6/lib/new_main15.rb:7:in `<main>' 

但我想輸出是爲:

current plan status is prime 

任何人都可以請幫助? 在此先感謝

回答

1

而不是檢查哪個td元素具有「當前」文本,我會建議檢查哪個無線電具有checked屬性。這減少了您不必擔心與之交互的元素數量。

table_row.radios.find(&:set?).value 

就可以檢查收音機的價值,看它是否與詞「加」或「黃金」開頭:

# Note that we scope to the tbody to ignore the header row. 
# Also make sure you do `trs` not `tds` for the rows. 
table_rows = browser.table(id: 'plans').tbody.trs 

# Iterate through the rows and check the checked radio button 
table_rows.each do |table_row| 
    case table_row.radios.find(&:set?).value 
    when /^plus/ 
    p 'current plan status is plus' 
    when /^prime/ 
    p 'current plan status is prime' 
    else 
    p 'current plan status is basic' 
    end 
end 

可以使用發現所選擇的無線電

需要注意的是舊版本的紅寶石(即1.9版),你需要使用查找所選無線電:

table_row.radios.find { |r| r.set? }.value 
+0

C:/Ruby193/lib/ruby/gems/1.9.1/gems/ watir-webdriver-0.6.11/lib/watir-webdriver/locators/element_locator.rb:165:in'check_type':預期的[String,Regexp]之一,爲true:TrueClass(TypeError) – test

+0

我得到了上述錯誤 – test

+0

該錯誤表示您正在使用Watir-Webdriver v0.6.11,這是從2014年開始的。所以是的,上述內容將不起作用,因爲它需要Watir v6.4(請注意,Watir-Webdriver寶石現在只是Watir寶石) 。我會更新答案以包含適用於舊版本的版本。 –