如何通过错误而不尝试在Python-Selenium中捕获?

问题描述:

在我的python-Selenium代码中,我应该使用很多try-catch来传递错误,如果我不使用它,我的脚本不会继续。 例如,如果我不使用尝试捕捉,如果我的脚本没有找到desc span我将有一个错误,我无法继续,如何通过错误而不尝试在Python-Selenium中捕获?

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 
+0

是的也许我的例子不好,我应该为定位器做什么? – parik

+0

对不起,我不了解你.. –