2017-07-30 25 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宝石) 。我会更新答案以包含适用于旧版本的版本。 –