简体   繁体   English

如果条件在硒webdriver C#中不起作用,请尝试捕获块

[英]try catch block with if condition is not working in selenium webdriver C#

if condition within try block does not work. if try块中的条件不起作用。

Script execution is always going out of the try catch loop even if the element (defined with Xpath) is present/displayed in the Web page. 即使Web页面中存在/显示了元素(用Xpath定义),脚本执行也总是脱离try catch循环。 if condition fails. if条件失败。

Any help will be appreciated. 任何帮助将不胜感激。

try
{
    string path = "//*@id='form_780_0']/div[2]/div/div[3]/div";
    if(driver.FindElement(By.XPath(path)).Displayed) 
    {
        Assert.Fail("Fails");
    }
}
catch (Exception)
{
    //There is nothing here
}

// Some code here and this is executed after evaluating if condition
// even if element is displayed

"Execution does not go into exception path either. it goes out of try catch loop after evaluating if condition" “执行也不会进入异常路径。它在评估是否满足条件后会退出try catch循环”

  • This means that the element is found, but it is NOT displayed. 这意味着已找到该元素,但未显示该元素。 If it was not found you would have gottten a NoSuchElementException. 如果找不到,您将获得NoSuchElementException。 Furthermore, if the element was found, and also displayed, you would trigger the Assert.Fail method 此外,如果找到并显示了该元素,则将触发Assert.Fail方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM