2017-06-19 79 views
0

我需要等待元素不可見 ,並找到了一些解決方案,但沒有一個似乎爲我工作。如何等待,直到一個元素不可見

我認爲這個問題是因爲我在使用PageObject模型。但我並不完全確定。

public static void WaitForElementToBeInvisible(this Browser browser, IWebElement element, int seconds = 30) 
     { 
      var wait = new WebDriverWait(browser.Driver, new TimeSpan(0, 0, seconds)); 
      wait.Until(ExpectedConditions.InvisibilityOfElementLocated(element)); 
     } 

但它返回錯誤提前

+0

你可以添加函數調用的代碼呢? – Murthi

+0

從這個例外中,你傳遞的是Iwebelement而不是By locator。 – Murthi

+1

你好戴蒙德,我已經做了大量的研究和編碼與硒c#自6個月以上,從我的知識這是不可能的。關於頁面對象設計模式的一些事情沒有意義,你應該嘗試用'By'對象來做到這一點。 –

回答

0

感謝所有的答案,但解決了這個問題。

public static void WaitUntilInvisible(this Browser browser, By element, int seconds = 30) 
     { 
      var wait = new WebDriverWait(browser.Driver, new TimeSpan(0, 0, seconds)); 
      wait.Until(ExpectedConditions.InvisibilityOfElementLocated(element)); 
     } 

然後用來作爲定位

By LoadingIcon = By.XPath(".//*[contains(@class, 'loading')]"); 

的作品就像一個魅力:)

1

簡短的回答cannot convert from IWebElement to Selenium.By

謝謝你,這是不可能的。 Selenium的開發人員已經決定沒有有用的用例。

如果By定位是可能的,你可以使用:

public static void WaitUntilInvisible(this By locator) 
     { 
      try 
      { 
       if (Driver.FindElement(locator).Displayed) 
       { 
        wait.Until(ExpectedConditions.ElementIsVisible(locator)); 
       } 
     } 

參見:Trying to convert IWebElement into a By element

1

你的代碼不正確。它調用InvisibilityOfElementLocated,它不接收IWebElement作爲參數。看我下面的例子。

public static bool WaitForElementToBeInvisible(this IWebElement element, int timeoutSecond = 10) 
{ 
    IWait<IWebElement> wait = new DefaultWait<IWebElement>(element); 
    wait.Timeout = TimeSpan.FromSeconds(timeoutSecond); 
    wait.PollingInterval = TimeSpan.FromMilliseconds(300); 
    try 
    { 
     wait.Until(!element.Displayed); 
    } 
    catch (WebDriverTimeoutException) 
    { 
     return false; 
    } 

    return true; 
} 

IWebElement div = driver.FindElement(By.Id("id")); 
var result = div.WaitForElementToBeInvisible(5); 
0
do { Playback.Wait(100); } while (_driver.FindElements(By.Id("elementId")).Count() > 0); 
相關問題