如何在 Python 的 <class> 中打印异常字符串

我在 Windows 10 上使用 Python 3.8.3。我有以下代码:


import # all the necessary modules


try:

    # do something

except WebDriverException:

    print(sys.exc_info()[0])

在异常情况下,我收到以下信息:


<class 'selenium.common.exceptions.SessionNotCreatedException'>

我如何才能print()只输出内的字符串<class>?:


selenium.common.exceptions.SessionNotCreatedException

任何帮助,将不胜感激。


慕标5832272
浏览 112回答 2
2回答

RISEBY

要获取异常的完整路径,请使用inspect.getmodule方法获取包名并使用type(..).__name __获取类名。except WebDriverException as ex:&nbsp;&nbsp; &nbsp; print (type(ex).__name__)对于全名,请尝试import inspect.....print(inspect.getmodule(ex).__name__, type(ex).__name__, sep='.')为了简单起见,您可以只解析已有的字符串print(str(sys.exc_info()[0])[8:-2]) # selenium.common.exceptions.SessionNotCreatedException

慕田峪9158850

也许你可以试试这个import # anythingtry:&nbsp; &nbsp;# somethingexcept Exception as e:&nbsp; &nbsp;print(e)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python