我在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
如有任何帮助,我们将不胜感激。
要获取异常的完整路径,请使用inspect.getmodule方法获取包名,并使用type(..(.__name __获取类名。
except WebDriverException as ex:
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
也许你可以试试这个
import # anything
try:
# something
except Exception as e:
print(e)