Ruby从HTML表中获取具有相同元素

时间:2017-07-30 15:33:44

标签: ruby watir

我有一个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>

我想通过Ruby 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 个答案:

答案 0 :(得分:1)

我建议不要检查哪个td元素有“当前”文本,而是检查哪个广播具有checked属性。这减少了您必须担心与之交互的元素数量。

您可以使用以下方式找到所选的电台:

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

然后,您可以检查收音机的值,看它是否以“plus”或“prime”开头:

# 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

请注意,对于旧版本的Ruby(即v1.9),您需要使用以下命令查找所选的电台:

table_row.radios.find { |r| r.set? }.value
相关问题