Java 捕获异常时对打印内容的更多控制

Java 捕获异常时对打印内容的更多控制,java,selenium-webdriver,printstacktrace,Java,Selenium Webdriver,Printstacktrace,当异常发生时,是否有方法从异常中获取特定信息 例如,当我调用printStackTrace时,我得到了很多信息……但有时我得到了太多的信息,而且大部分信息都不是我需要看到的东西,因为它们总是相同的信息 至少当谈到SeleniumWebDriver时,它首先告诉我问题出在哪里 com.thoughtworks.selenium.SeleniumException: Element blah not found at org.openqa.selenium.internal.seleniumem

当异常发生时,是否有方法从异常中获取特定信息

例如,当我调用
printStackTrace
时,我得到了很多信息……但有时我得到了太多的信息,而且大部分信息都不是我需要看到的东西,因为它们总是相同的信息

至少当谈到SeleniumWebDriver时,它首先告诉我问题出在哪里

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

这是没有用的。我想至少看看它来自哪一行。

当然,只获取前几个堆栈帧并解析它们。使用
getStackTrace
获取一个
StackTraceElement[]
并对其进行迭代,直到在包中获得一个帧(或以其他方式进行筛选)以获得正确的行。Selenium内部线路可能不是您需要的

在这个元素上,您可以调用
getMethodName
getLineNumber