C# Selenium 等待元素显示等于 none - 仅适用于调试

C# Selenium wait until element display is equal to none - works only on debug

我在自动化包含 div 和 style="display: none;" 的表单时遇到了一些麻烦 它有一个 Id,但我不能在没有调试的情况下使用它。

问题是:有一个下拉菜单仅在此 "load image" 消失后才加载其选项。 所以我尝试了很多解决方案和 SeleniumExtras.WaitHelpers 中的每个选项(因为旧的将在 C# 中被弃用)。

我从解决方案中得到的越接近这里的代码:

这是我的代码:

<div id="updateProgress" style="display: none;" role="status" aria-hidden="true" xpath="1">

            <div id="divLoading" style="position: absolute; z-index: 10; left: 450px; top: 90px;">
                <img id="ajaxLoadNotificationImage" src="../images/ajax-loader-center.gif" alt="[image]">
            </div>

</div>

--

 WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(90));
            wait.Until(WebDriver =>
            {
                return WebDriver.FindElement(By.Id("updateProgress")).GetAttribute("style").Contains("display: none;");
            });

我也试过这些:

wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeSelected(By.XPath(profilePage.LoadingStateImg.GetAttribute("style").Contains("display: none;").ToString())));

wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(By.XPath(profilePage.LoadingStateImg.GetAttribute("style").Contains("display: none;").ToString())));

上面没有列出,但我试过了: - InvisibilityOfElementWithText - StalenessOf - 元素可见 - TextToBePresentInElementValue

运行 似乎甚至没有进入这些代码行。 调试显示带有属性 "display: none;" 的文本 ok 并转到所需下拉列表的选择。 我不想使用 Thread.Sleep(),但到目前为止它是唯一有效的方法。 *(Tread.Sleep 有效,但显然我不想进入它,因为页面太慢,我需要 40-60 秒...)

提前致谢!

我自己解决了。 首先需要检查元素是否可见(通过定位器),这样我就可以检查它是否不可见。该元素在页面上明确显示之前可能会在页面上闪烁:

WebDriver.WaitForElementIsVisible(By.Id(profilePage.LoadingStateLocator));
WebDriver.WaitForElementIsNotVisible(profilePage.LoadingStateImg);