Java try-catch没有捕获异常

时间:2014-01-28 00:05:43

标签: java exception selenium try-catch

我有以下方法:

private void checkIfLoggedIn() {
  try{
    try {
      new WebDriverWait(driver, 10)
            .until(ExpectedConditions.visibilityOfElementLocated(
             By.cssSelector("a[href*='score']")));
    } 
    catch (WebDriverException e) {
    } 
    loggedInState = 2;
  }
  catch (TimeoutException e) {
    loggedInState = -1;
  }
}

简单地说,这个方法的工作是等到网页完成加载已登录的pa ge,通过更改变量让其余的代码知道。此代码在绝大多数情况下都能正常运行,但是如果驱动程序在完成之前关闭(由另一个线程),它就会崩溃。代码停止我完全没问题 - 如果网页已经关闭,它很可能已完成任务(或由于其他原因而崩溃)。问题是错误日志(下面发布)不会消失。我甚至尝试过像catch (Exception e)这样广泛的东西来捕获任何可能的异常,但是崩溃日志不会消失。任何建议将不胜感激!

编辑:感谢@jdigital,我发现代码实际上是捕获异常,但是Selenium输出它自己的异常会掩盖它。关于如何让硒停止输出错误的任何建议都将不胜感激!

 Jan 27, 2014 6:48:29 PM org.openqa.selenium.support.ui.ExpectedConditions findElement
WARNING: WebDriverException thrown by findElement(By.selector: a[href*='score'])
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15'
System info: host: '[Edited out for Privacy]', ip: '[Edited out for Privacy]', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_45'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:396)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:432)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:730)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:728)
    at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:130)
    at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)
    at Web.WebCrawler.checkIfLoggedIn(WebCrawler.java:89)
    at Web.WebCrawler.access$0(WebCrawler.java:86)
    at Web.WebCrawler$1.run(WebCrawler.java:80)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:178)
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:144)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:131)
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at org.openqa.selenium.remote.HttpCommandExecutor.fallBackExecute(HttpCommandExecutor.java:319)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:298)
    at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:165)
    at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:366)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527)
    ... 13 more

1 个答案:

答案 0 :(得分:0)

您应该能够设置Selenium's Logging Preferences来自定义异常输出。

编辑: 也许上面引用的链接中的说明不太有用。 看起来Selenium使用标准的Java记录器接口。抓住RemoteWebDriver对象,然后通过setLogLevel(java.util.logging.Level.OFF)关闭其日志记录。

另一种选择,FirefoxDriver允许将stdout / stderr重定向到文件。这样做的好处是可以在需要时检查输出;否则,你可以覆盖它或删除它。