3

您如何找到定义自定义异常的位置以便您可以导入它。在我的代码中引发的错误的堆栈跟踪中,我得到了 NoSuchElementException。我想捕捉这个特定的异常,但我找不到从哪里导入它。有没有办法确定从堆栈跟踪中导入什么?

Traceback (most recent call last):
  File "/home/ubuntu/webapps/tablecloth/src/tablecloth/apps/atsa/adaptor.py", line 266, in login
    welcome_field = driver.find_element_by_class_name(self.welcome_id)
  File "/home/ubuntu/webapps/tablecloth/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 342, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "/home/ubuntu/webapps/tablecloth/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 680, in find_element
    {'using': by, 'value': value})['value']
  File "/home/ubuntu/webapps/tablecloth/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
    self.error_handler.check_response(response)
  File "/home/ubuntu/webapps/tablecloth/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 158, in check_response
    raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u'Unable to locate element: {"method":"class name","selector":"userName"}' ; Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ 
4

1 回答 1

9

捕获异常,Exception以便您可以访问异常对象:

try:
    call_your_function()
except Exception as e:
    print e.__module__

然后用Exception你找到的更具体的替换。

于 2013-11-01T00:55:09.173 回答