我在 Windows 10 上使用 Python 3.8.3。我有以下代碼:import # all the necessary modulestry: # do somethingexcept WebDriverException: print(sys.exc_info()[0])在異常情況下,我收到以下信息:<class 'selenium.common.exceptions.SessionNotCreatedException'>我如何才能print()只輸出內的字符串<class>?:selenium.common.exceptions.SessionNotCreatedException任何幫助,將不勝感激。
2 回答

RISEBY
TA貢獻1856條經驗 獲得超5個贊
要獲取異常的完整路徑,請使用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

慕田峪9158850
TA貢獻1794條經驗 獲得超7個贊
也許你可以試試這個
import # anything
try:
# something
except Exception as e:
print(e)
添加回答
舉報
0/150
提交
取消