gpt4 book ai didi

Java 未在 catch block 中捕获异常

转载 作者:行者123 更新时间:2023-12-02 02:32:09 25 4
gpt4 key购买 nike

我正在尝试使用 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 block 中放入什么内容,都会引发 org.openqa.selenium.NoSuchElementException 。就好像它被简单地忽略了一样。该程序甚至从未到达第二个 block 。我尝试将 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)

最佳答案

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

By.xpath() 不执行任何操作,驱动程序使用它来访问元素。

关于Java 未在 catch block 中捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46936139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com