2014-02-11 107 views
3

我如何告訴Selenium webdriver等待特定元素在css中設置特定屬性?如何等待css屬性改變?

我想等待: element.getCssValue("display").equalsIgnoreCase("block")

通常一個等待元素存在這樣的: webdriver.wait().until(ExpectedConditions.presenceOfElementLocated(By.id("some_input")));

我如何可以等待display屬性的特定的CSS值?

回答

10

我認爲這會對你有效。

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

對上述答案的小修改對我有幫助。我不得不用兩個(的I開始By.XPATH,沒有前1:

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

這個答案是Python的,因爲僅供參考 – iceui2

0

在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;");