有没有办法分离出因异常原因而提供的信息?

Is there a way to separate out information provided in cause for exceptions?

当我打印由 NoSuchElementException 引起的 TimeoutException 原因时,如:

catch (TimeoutException tox) {
            tox.printStackTrace();
            printWarn("Cause for failure => " + tox.getCause());
}

我从中得到的输出如下:

Cause for failure => org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 10.41 seconds..

Build info: version: '2.48.2', revision: '41bccdd10cf2c0560f637404c2d96164b67d9d67', time: '2015-10-09 13:08:06' System info: host: 'localhost', ip: '172.20.44.84', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.10.5', java.version: '1.8.0_65'

Driver info: AppiumDriver

Capabilities [{....}]

Session ID: 405d7843-5353-4a96-9288-b6d8301651b5

* **Element info: {Using=id, value=et_mobile_editTextView}

我可以使用任何 属性 或附加任何我可能不知道的方法来单独获取所有粗体信息吗?

我目前拥有的是:

String completeCause = tox.getCause();

我要找的是:

String buildInfo = tox.<somemethod>();
String driverInfo = tox.<somemethod>(); etc..

在此先感谢您抽出时间来帮助我。

TimeOutException 的 Selenium JavaDoc 显示了方法 getAdditionalInformationgetBuildInformationgetDriverNamegetSystemInformation,它们将分别为您提供所有需要的信息。

catch(TimeoutException tox) {
  tox.printStackTrace();
  String driverName = tox.getDriverName();
  BuildInfo buildInfo = tox.getBuildInformation();
  ...
}

元素信息隐藏在 getAdditionalInformation 中。查看 WebDriverExceptionsource code(您的异常是从中继承的)信息存储在私有映射中。但是,信息由 \n 分隔,这将使您可以过滤这些条目并仅搜索具有 Element Info.

的条目

它可能看起来像这样(没有测试,只是写下来)

String[] additionalInformation = tox.getAdditionalInformation().split("\n");

for(String s : additionalInformation) {
  String[] part = s.split(":");
  if("Element Info".equals(part[0]) {
    String elementInfo = part[...]; //you need to investigate the right output of the split
  }
}

[旧post]

如我所见,TimeOutException.getCause() 将 return 变成 Throwable,在本例中是 NoSuchElementExceptionSeleniums JavaDoc 显示有方法 getDriverNamegetSystemInformation.

catch (TimeoutException tox) {
    tox.printStackTrace();
    if(tox.getCause() instanceof NoSuchElementException) {
      NoSuchElementException nse = (NoSuchElementException) tox.getCause();
      ...
    }
}