This script runs in irb, but not as a standalone script:
req开发者_开发问答uire 'watir'
b = Watir::Browser.start "http://www.google.com/"
b.text_field(:name => "q").value = "foo"
b.button(:name => "btnG").click
b.link(:url => /foofighters/).click
The last line is where it fails when run outside of irb (with a 'element not found' error message). But works just fine when run from irb. Could this be a timing problem?
Thanks in advance!
You need to use .when_present to make sure it waits until it is present
require 'rubygems'
require 'watir'
b = Watir::Browser.start "http://www.google.com/"
b.text_field(:name => "q").value = "foo fighters"
b.button(:name => "btnG").click
b.link(:url => /foofighters/).when_present.click
Any time something works when you execute the steps one by one using IRB, but fails when run as a script, the first thing to think is 'timing issue' so your instincts on that are right on the mark. (timing is after all the biggest difference if the code being executed is the same in both cases)
Alister suggests one potential solution to fixing the timing issue. Other examples can be found in the watir-wiki under the 'how to wait with watir' topic
精彩评论