硒C#尝试/捕获帮助

时间:2018-11-19 15:41:38

标签: c# selenium automated-tests

我试图在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 
        }

2 个答案:

答案 0 :(得分:1)

 X <- cbind(1, matrix(1:10))
 b<-solve(t(X)%*%X)%*%t(X)%*%y

答案 1 :(得分:0)

好吧,我不确定这是否是您想要的,因为您在文本中询问的内容与您的代码不同。该代码捕获所有异常,并且如果异常是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(Exception ex)
{
    //Catches every exception
    if(ex is NoSuchElementException)
    {
        //Do nothing, if there's no icon your code will continue as if nothing happened
        //Or throw a custom exception for this case
    }
    else
    {
        //If there's an icon throw the exception
        //Here you can throw a custom exception
        throw ex;
    }
}