【问题标题】:How to wait for a css attribute to change?如何等待css属性改变?
【发布时间】:2019-09-16 18:47:57
【问题描述】:

如何告诉 Selenium webdriver 等待特定元素在 css 中设置特定属性?

我要等:

element.getCssValue("display").equalsIgnoreCase("block")

通常会像这样等待元素出现:

webdriver.wait().until(ExpectedConditions.presenceOfElementLocated(By.id("some_input")));

如何等待display属性的具体css值?

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    我认为这对你有用。

    webdriver.wait().until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='some_input'][contains(@style, 'display: block')]")));
    

    【讨论】:

    • 我试过了,但没用 //span[contains(text(),'Create Professor')][contains(@style,'color: #222222')] 。它给出了未找到元素的异常。 HTML 是<span class="menu-item" id="professor-create">Click here to Create Professor...</span>。 Create Profressor 从禁用(灰色文本)变为黑色粗体文本
    • 我很确定这不会起作用,因为 JS 对样式的更改不会更改 @style 的值。 OP 只需要 element.getCssValue("display") 上的一个普通的旧等待循环。
    【解决方案2】:

    对上述答案的小修改对我有帮助。在开始 By.XPATH 之前,我必须使用两个 (s,而不是 1 个:

    WebDriverWait(browser,1000).until(EC.presence_of_element_located((By.XPATH,xpathstring)))
    

    【讨论】:

    • 此答案适用于 Python,仅供参考
    【解决方案3】:

    在带有扩展方法的 C# 中:

    public static WebDriverWait wait = new WebDriverWait(SeleniumInfo.Driver, TimeSpan.FromSeconds(20));
        public static void WaitUntilAttributeValueEquals(this IWebElement webElement, String attributeName, String attributeValue)
            {            
                    wait.Until<IWebElement>((d) =>
                    {
                        if (webElement.GetAttribute(attributeName) == attributeValue)
                        {
                            return webElement;
                        }
                        return null;
                    });
            }
    

    用法:

    IWebElement x = SeleniumInfo.Driver.FindElement(By.Xpath("//..."));
    x.WaitUntilAttributeValueEquals("disabled", null); //Verifies that the attribute "disabled" does not exist
    x.WaitUntilAttributeValueEquals("style", "display: none;");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 2022-12-11
      • 2016-01-02
      相关资源
      最近更新 更多