簡體   English   中英

Java未在catch塊中捕獲異常

[英]Java not catching Exception in a catch block

我正在嘗試實現一種使用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替換為捕獲,但是行為保持不變。

是什么原因造成的,我該如何解決?

堆棧跟蹤:

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)

您在錯誤的地方抓到它。 真正的事情發生在CommonSeleniumStepDefinitions.java:206中

By.xpath()不執行任何操作,驅動程序使用它來訪問元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM