ie.link(:id, "ctl00_ContentPlaceHolder1_BtnSearch").click
rescue Timeout::Error
#sleep(5)
puts "timeout"
ie.close
#sleep(9)
retry #open new browser and go to begin
end`
当.click链接超时,然后输出=超时,但ie.close不起作用。 并且出现超时错误 * 我想在出现超时错误时关闭浏览器 *
答案 0 :(得分:2)
我不相信ie.link(:id, "ctl00_ContentPlaceHolder1_BtnSearch").click
会抛出Timeout::Error
。这就是永远不会执行rescue
块的原因。
抛出的可能异常是:
ie.link(:id, "ctl00_ContentPlaceHolder1_BtnSearch").click
并且找不到元素时,会出现Watir::Exception::UnknownObjectException
。ie.link(:id, "ctl00_ContentPlaceHolder1_BtnSearch").when_present.click
并且在所需时间范围内找不到该元素时,会出现Watir::Wait::TimeoutError
。您的救援可能需要抓住其中一个例外。
begin
ie = Watir::Browser.new
ie.goto 'www.yourpage.com'
ie.link(:id, "ctl00_ContentPlaceHolder1_BtnSearch").click
rescue Watir::Exception::UnknownObjectException
puts "element not found"
ie.close
retry #open new browser and go to begin
end
或者如果您在元素上使用when_present
:
begin
ie = Watir::Browser.new
ie.goto 'www.yourpage.com'
ie.link(:id, "ctl00_ContentPlaceHolder1_BtnSearch").when_presentclick
rescue Watir::Wait::TimeoutError
puts "element did not appear in time"
ie.close
retry #open new browser and go to begin
end