2017-07-30 3 views
0

同じ要素を持つHTMLテーブルから値を取得しますRubyは以下のようになり、私は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ワチールてページ内の現在の計画である計画をチェックしたいです。以下はスクリプトです:

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 

あなたが使用して、選択したラジオを見つけることができますルビーの古いバージョン(すなわちV1.9)のために、あなたが使用して選択したラジオを見つけることが必要になること

注:

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

C:/Ruby193/lib/ruby/gems/1.9.1/gems/ワチール-webdriverを-0.6.11/LIB /ワチール-webdriverを/ロケータ/ element_locator.rb:165: 'check_type 'で:[文字列、正規表現]のいずれかの予想は、真得た:私は上記のエラーを持っ – test

+0

TrueClass(TypeError例外)を – test

+0

このエラーは、2014年のWatir-Webdriver v0.6.11を使用していると言います。したがって、Watir-Webdriverの宝石はWatirの宝石であることに注意してください。 。古いバージョンで動作するはずのバージョンを含めるように答えを更新します。 –

関連する問題