【发布时间】:2018-11-20 14:46:43
【问题描述】:
我正在尝试在 Selenium C# 中编写一个 try/catch,如果 Web 元素不存在,则捕获 NoSuchElementException,如果元素存在,则抛出自定义异常。编码非常绿色,因此将不胜感激所有帮助。谢谢!
try
{
IWebElement spIcon = driver.FindElement(By.CssSelector("#gridview-1080-record-2658335 > td.x-grid-cell.x-grid-td.x-grid-cell-headerId-propertiesColInv.wrappable.icon-spacer.x-unselectable.wrappable.icon-spacer > div > i"));
}
catch (NoSuchElementException spIcoNotDisplayed)
{
//if spIcon is NOT present;
//then continue;
//else throw custom exception
}
【问题讨论】:
-
为什么要在图标出现时抛出自定义异常?在这种情况下,它将如何进入错误处理程序?当然,如果 Icon 存在,FindElement 可以正常工作并且不会引发异常。
-
为什么不捕获所有异常然后做一些
if (ex is NoSuchElementException){ //Continue;}else{throw ex;}
标签: c# selenium automated-tests