Sargon1
Sargon1

Reputation: 874

Java not catching Exception in a catch block

I'm trying to implement a method for automated tests with Selenium and Cucumber, which currently looks like this:

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']");
    }
}

What it's supposed to do is to map the word "fromAccount" in Gherkin to HTML elements that are looked up by their respective XPath expressions. If the method doesn't find the element by one expression, it's supposed to not throw the org.openqa.selenium.NoSuchElementException it would normally throw in such a case, an instead attempt the search with the next expression.

However, the org.openqa.selenium.NoSuchElementException is thrown regardless of what I put in the catch block. It's as if it was simply ignored. The program never even gets to the second block. I tried to substitute org.openqa.selenium.NoSuchElementException into the catch, but the behavior stayed the same.

What's causing this and how do I fix it?

Stack Trace:

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)

Upvotes: 0

Views: 1056

Answers (1)

Admit
Admit

Reputation: 4987

You are catching it in a wrong place. The real thing happens in the CommonSeleniumStepDefinitions.java:206

By.xpath() does nothing, driver uses it to access an element.

Upvotes: 2

Related Questions