[英]How to pass error without try catch in Python-Selenium?
在我的python-Selenium代碼中,我應該使用許多try-catch來傳遞錯誤,並且如果我不使用它,則腳本不會繼續。 例如,如果我不使用try catch,如果我的腳本未找到desc span
則會出現錯誤並且無法繼續,
try :
libelle1produit = prod.find_element_by_css_selector(('.desc span')).text # libelle1produit OK
except:
pass
是否有傳遞錯誤的替代方法?
是否有傳遞錯誤的替代方法?
是的,為避免try-catch
並避免錯誤,請嘗試使用find_elements
代替,它會返回具有匹配定位符的所有元素的列表,或者如果找不到任何內容,則返回空列表,您只需要檢查列表長度即可確定該元素是否存在或不是如下:
libelle1produit = prod.find_elements_by_css_selector('.desc span')
if len(libelle1produit) > 0 :
libelle1produit[0].text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.