在PythonSelenium中如何传递错误而不尝试捕捉?

2024-09-30 10:32:37 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的python Selenium代码中,我应该使用manytry-catch来传递错误,如果不使用它,我的脚本将无法继续。 例如,如果我不使用try-catch,如果我的脚本没有找到desc span,我将出现一个错误,我无法继续

    try :
        libelle1produit = prod.find_element_by_css_selector(('.desc span')).text  # libelle1produit OK
    except:
        pass

有没有传递错误的替代方法?在


Tags: 代码脚本byselenium错误prodelementfind
1条回答
网友
1楼 · 发布于 2024-09-30 10:32:37

is there an alternative method for passing error?

是的,为了避免try-catch并避免错误,请尝试使用find_elements来代替它,它将返回带有匹配定位器的所有元素的列表或空列表(如果找不到任何内容),则只需检查列表长度以确定元素是否存在,如下所示:

libelle1produit = prod.find_elements_by_css_selector('.desc span')

if len(libelle1produit) > 0 :
  libelle1produit[0].text

相关问题 更多 >

    热门问题