单击带有IF的网络元素时如何处理ElementClickInterceptedException

时间:2019-09-04 11:04:35

标签: c# selenium-webdriver error-handling

我有此块UI叠加层,有时当我尝试单击某个项目(按钮或其他任何东西)时,它会叠加或遮盖此操作,因此,每当我尝试单击时,我都会收到此ElementClickInterceptedException。元素被遮盖。

我想做一个假设,如果收到此错误,请等待,直到此BLOCK UI类消失,然后尝试再次单击它。

但是按照这种逻辑,仍然会收到错误,并且框架无法继续,抛出错误并传递给下一个TestCase

if (driver.FindElement(By.ClassName("block-ui-overlay")).Displayed)
{
    WebDriverWait waitForElement = new WebDriverWait(driver, TimeSpan.FromSeconds(5000));
    waitForElement.Until(ExpectedConditions.InvisibilityOfElementLocated(By.ClassName("blockUI blockOverlay")));
}
managedg.MAN_DistGroups.Click();

Fluentwait:

public static void BlockUIWait(IWebDriver driver , string selector)
{
      DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(driver);
      fluentWait.Timeout = TimeSpan.FromSeconds(5);
      fluentWait.PollingInterval = TimeSpan.FromMilliseconds(150);
      fluentWait.IgnoreExceptionTypes(typeof(ElementClickInterceptedException));
      fluentWait.Until(ExpectedConditions.ElementToBeClickable(driver.FindElement(By.CssSelector(selector))));

结果消息:

OpenQA.Selenium.ElementClickInterceptedException:元素<div class="ng-scope">在点(404,613)不可点击,因为另一个元素<div class="blockUI blockOverlay">遮盖了它

2 个答案:

答案 0 :(得分:1)

您可以为此使用FluentWait。在Fluent Wait中,您可以等待条件并忽略在该等待期间发生的特定异常

   Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(ElementClickInterceptedException.class);

   WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }
   });

有关更多详细信息,请参见https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html

以下是c#实现

DefaultWait<IWebDriver> fluentWait = new DefaultWait<IWebDriver>(webdriver);
            fluentWait.Timeout = TimeSpan.FromSeconds(5);
            fluentWait.PollingInterval = TimeSpan.FromMilliseconds(250);
            fluentWait.IgnoreExceptionTypes(typeof(NoSuchElementException));
            IWebElement searchResult = fluentWait.Until(x => x.FindElement(By.Id("search_result")));

答案 1 :(得分:-1)

由于UI总是出现,因此您可以等待它出现,然后等待它消失。假设您的定位器正确,那应该可以...

WebDriverWait waitForElement = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); // changed from 5000 because this is seconds and not milliseconds
By blockUI = By.CssSelector(".blockUI.blockOverlay");
waitForElement.Until(ExpectedConditions.VisibilityOfElementLocated(blockUI));
waitForElement.Until(ExpectedConditions.InvisibilityOfElementLocated(blockUI));
managedg.MAN_DistGroups.Click();

我的假设是您的定位器应该相同,但是我看不到该页面,因此无法对其进行测试。鉴于您提供的错误消息,我认为在第一次等待时也应使用第二个定位器。