当找不到元素时,如何避免/忽略OpenQA.Selenium.NoSuchElementException?

问题描述 投票:0回答:1

我想确保给定元素不在DOM中,或者如果它存在于DOM中,则不再显示。

为了这样做,我将WebDriverWait配置为忽略NoSuchElementException和:

public static void WaitForElementToDisappear(IWebDriver driver)
{
     var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
     wait.IgnoreExceptionTypes(typeof(OpenQA.Selenium.NoSuchElementException));
     wait.Until(d => d.FindElement(By.ClassName("ClassName")).Displayed == false);
}

不幸的是,这种兴奋并没有被忽略,一旦抛出,测试就会失败。

如何处理?

编辑:我发现不幸的是,这是期望的等待行为,直到超时都不会引发异常,所以我的方法是绝对错误的。

.net selenium exception webdriverwait
1个回答
0
投票

您为什么不使用现有的ExpectedConditions

wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.ClassName("ClassName")));
© www.soinside.com 2019 - 2024. All rights reserved.