ruby - Watir fails to find element intermittently -


i have following code using watir check boxes on javascript form.

nba_north_america = 'https://sports.bwin.com/en/sports#leagueids=6004&sportid=7' money_line = 'cat_43' totals = 'cat_48'  def method      browser = watir::browser.new      browser.goto nba_north_america     browser.checkbox(:id => totals).parent.span.click unless browser.checkbox(:id => totals).checked?     browser.checkbox(:id => money_line).parent.span.click unless browser.checkbox(:id => money_line).checked? == false      browser.divs(:class => 'layout4').each |event|      end     browser.close end 

it works half time other half throws error:

/var/lib/gems/1.9.1/gems/watir-webdriver-0.9.1/lib/watir-webdriver/elements/element.rb:536:in `assert_element_found': unable locate element, using {:tag_name=>"span"} (watir::exception::unknownobjectexception) 

on line second checkbox (money line). inspecting in firefox can see checkbox , span elements both exist. what's happening here?

usually these race conditions caused asynchronous nature of javascript. add wait this:

browser.checkbox(:id => totals).when_present.checked? 

Comments