【问题标题】:C# Selenium wait until element display is equal to none - works only on debugC# Selenium 等到元素显示等于无 - 仅适用于调试
【发布时间】:2020-02-29 00:04:02
【问题描述】:

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

问题是:只有在此“加载图像”消失后,才会加载其选项的下拉列表。 所以我尝试了很多解决方案和SeleniumExtras.WaitHelpers 中的每一个选项(因为旧的将在 C# 中被弃用)。

我从解决方案中得到的更接近的是这里的代码: https://stackoverflow.com/a/58100882/11670912

这是我的代码:

<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 - 陈旧的 - 元素可见 - TextToBePresentInElementValue

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

提前致谢!

【问题讨论】:

  • 如果我理解正确,你有这个 div,当它消失时,列表会加载?也许你应该等待它“不可见”而不是依赖于“样式”属性。
  • 我也试过这个:wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(By.XPath("//div[@id='updateProgress'][@style='display: none;']"))); 没用。

标签: c# selenium-webdriver webdriverwait


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 2020-06-12
    • 1970-01-01
    • 2019-06-27
    相关资源
    最近更新 更多