【问题标题】:More control over what is printed out when an exception is caught对捕获异常时的打印内容进行更多控制
【发布时间】:2023-03-17 02:15:02
【问题描述】:

有没有办法在异常发生时从异常中获取特定信息?

例如,当我致电printStackTrace 时,我会得到很多信息……但有时我会得到太多信息,而其中大部分不是我需要查看的内容,因为无论如何它总是相同的信息。

至少在谈到 Selenium Webdriver 时,它首先告诉我问题是什么

com.thoughtworks.selenium.SeleniumException: Element blah not found
  at org.openqa.selenium.internal.seleniumemulation.ElementFinder.findElement(ElementFinder.java:92)
  at org.openqa.selenium.internal.seleniumemulation.Click.handleSeleneseCommand(Click.java:35)
  at org.openqa.selenium.internal.seleniumemulation.Click.handleSeleneseCommand(Click.java:1)
  at org.openqa.selenium.internal.seleniumemulation.SeleneseCommand.apply(SeleneseCommand.java:35)
  at org.openqa.selenium.internal.seleniumemulation.Timer.run(Timer.java:40)
  at org.openqa.selenium.WebDriverCommandProcessor.execute(WebDriverCommandProcessor.java:140)  
  ...
Caused by: org.openqa.selenium.WebDriverException: ...
...
...
...

然后它继续告诉我有关我的环境、能力(以及另一个消息列表)以及更多“由”信息。

这是一条很长的消息,我不希望它弄乱我的日志文件。 有没有办法省略所有“由”相关消息?

我试过getMessage 但它只是说

Element blah not found

这没用。我想至少看看它来自哪条线。

【问题讨论】:

    标签: java selenium-webdriver printstacktrace


    【解决方案1】:

    当然,只获取前几个堆栈帧并解析它们。使用getStackTrace 获取StackTraceElement[] 并遍历它们,直到您在您的包 中获得一个框架(或者您想要过滤的其他内容)以获得正确的行。 Selenium 内部线路可能不是您需要的。

    在此元素上,您可以调用 getMethodNamegetLineNumberthe like

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 2015-06-10
      • 1970-01-01
      • 2011-09-25
      • 2013-09-28
      • 2017-12-27
      • 2014-02-12
      相关资源
      最近更新 更多