如何在使用 IF 单击Web元素时处理 ElementClickInterceptedException



我有这个块UI覆盖,有时当我尝试单击一个项目(按钮或任何东西(时,它会叠加或掩盖此操作,所以每当我尝试单击时,我都会收到这个ElementClickInterceptedException。元素模糊。

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

但是有了这个逻辑,错误仍然被接收,框架无法继续,抛出错误并传递给下一个测试用例

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();

流利等待:

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">遮挡了它

您可以使用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")));

由于 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();

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