Java没有在catch块中捕获异常

时间:2017-10-25 15:16:14

标签: java selenium exception try-catch

我正在尝试使用Selenium和Cucumber实现自动化测试的方法,目前看起来像这样:

public By getFromAccountSelector() {
    try {
        return By.xpath("//ib-dropdown[@ng-model='model.from']");
    } catch (Exception exc) {
        return By.xpath("//ib-dropdown[@ng-model='data.account']");
    }
}

它应该做的是将Gherkin中的“fromAccount”一词映射到由各自的XPath表达式查找的HTML元素。如果该方法没有通过一个表达式找到该元素,那么它应该不会抛出通常在这种情况下抛出的org.openqa.selenium.NoSuchElementException,而是尝试使用下一个表达式进行搜索。

但是,无论我在catch块中放置什么,都会抛出org.openqa.selenium.NoSuchElementException。这就好像被忽略了一样。该计划甚至没有到达第二个街区。我试图将org.openqa.selenium.NoSuchElementException替换为catch,但行为保持不变。

造成这种情况的原因是什么?如何解决?

堆栈追踪:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//ib-dropdown[@ng-model='model.from']"}
  (Session info: chrome=62.0.3202.62)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 367 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'GHKY9H2', ip: '192.168.56.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f), userDataDir=C:\Users\marecek\AppData\Local\Google\Chrome\User Data\Selenium\}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=62.0.3202.62, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: 32332850f941b37dcb9ce6eaae527ea5
*** Element info: {Using=xpath, value=//ib-dropdown[@ng-model='model.from']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:509)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
    at cz.bsc.tbcselenium.steps.common.CommonSeleniumStepDefinitions.click_combo_and_select(CommonSeleniumStepDefinitions.java:206)
    at ?.When click combo "fromAccount" and select item 3(src/main/resources/features/Test.feature:14)

1 个答案:

答案 0 :(得分:2)

你在错误的地方捕捉它。真实的事情发生在CommonSeleniumStepDefinitions.java:206

By.xpath()什么都不做,驱动程序使用它来访问元素。