2013-04-18 26 views
0
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不起作用。 且超時錯誤出現 * 我要關閉瀏覽器時超時錯誤出現 *超時ie.close不工作,並超時錯誤發生

+1

「*我要關閉瀏覽器時超時錯誤出現*」你什麼呢?就像某人所在的瀏覽器一樣? _爲什麼_? – Cubic

+1

我想要一個無限循環,如果超時發生。直到我有慾望output.after超時, 瀏覽器應該是關閉,然後再次打開新的瀏覽器。 並且這個過程應該重複直到需要輸出。 – unknownbits

+0

我編輯了我的問題更清楚。 – unknownbits

回答

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