簡體   English   中英

嘗試除繼續Python無法正常工作

[英]Try Except Continue Python not working

我有以下嘗試例程:

    While true:
    print("back to beginning")  
    self.driver.get("http://mylovelywebsite.com")
        try:
            wait.until(
                EC.title_is("This is my title")
            )
        except TimeoutException as ex:
            print("HERE!")
            print(ex.message)
            self.driver.quit()
            continue

它用於硒,只是等待標題是否存在。 但是,我相信這只是一個python問題(硒工作正常)

發生的問題是ex.message沒有打印出任何內容。 但是,即使我刪除了它,它也不會轉到.quit()函數,而在到達continue語句時,它只會返回到print(“ HERE!”)語句(而不是返回到開頭)。劇本。

我想知道如何制作它,以便在出現錯誤時將其返回到腳本的開頭並再次運行? 我是否必須減少continue和quit()1的縮進量? 我不確定這是否行得通,因為即使沒有捕獲到錯誤,它也會進入continue語句。

  1. 您的縮進不正確。
  2. Whilewhile
  3. true應該為True
  4. TimeoutException沒有message屬性; 請改用str(ex)
  5. 如果已導入TimeoutException則沒有顯示。 from selenium.common.exceptions import TimeoutException

正確格式

from selenium.common.exceptions import TimeoutException

while True:
    print("back to beginning")  
    self.driver.get("http://mylovelywebsite.com")
    try:
        wait.until(EC.title_is("This is my title"))
    except TimeoutException as ex:
        print("HERE!")
        print(str(ex))
        self.driver.quit()
        continue

樣例程序可以滿足您的描述

from selenium.common.exceptions import TimeoutException

timeOut = True 

while True:
    print("back to beginning")  
    try:
        if timeOut: 
            raise TimeoutException("Something caused a timeout")
        else:
            break # leave the while loop because no error occurred
    except TimeoutException as ex:
        print("HERE!")
        print(str(ex))
        continue

無限循環; ctrl+c終止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM