2017-07-17 31 views
0

我一直致力於使Selenium Framework成爲頁面工廠,但是我一直在努力獲得在我的擴展類中工作的Wait.Until命令。無法從OpenQA.Selenium.IWebElement轉換爲Open.Qa.Selenium.By

public static void Wait(this IWebElement element, IWebDriver driver, float TimeOut) 
{ 
    WebDriverWait Wait = new WebDriverWait(driver, TimeSpan.FromSeconds(TimeOut)); 
    return Wait.Until(ExpectedConditions.ElementIsVisible(element)); 
} 

如果我用上面的代碼中,我得到的錯誤 無法從OpenQA.Selenium.IWebElement轉換爲Open.Qa.Selenium.By

任何建議,我怎麼能修改上面的代碼,使它在我使用的模型中工作?

回答

1

There is no ExpectedConditions.ElementIsVisible(IWebElement)。不幸的是,你只能使用ElementIsVisibleBy對象。

如果合適,您可以使用substitute with ExpectedConditions.ElementToBeClickable(IWebElement),這是一種稍微不同的情況,它也檢查除了可見之外還啓用了該元素。但這可能會滿足您的要求。

或者,你可以只調用element.Displayed在自定義WebDriverWait,確保忽略或趕上NoElementException

這是一個古老的實現這一點,我已經使用並改變了你的情況,有可能是一個更清潔現在的方式來做到這一點:

new WebDriverWait(driver, TimeSpan.FromSeconds(TimeOut)) 
{ 
    Message = "Element was not displayed within timeout of " + TimeOut + " seconds" 
}.Until(d => 
{ 
    try 
    { 
     return element.Displayed; 
    } 
    catch(NoSuchElementException) 
    { 
     return false; 
    } 
} 

上面代碼的簡單說明......它會嘗試一遍又一遍執行element.Displayed直到它返回true。當element不存在時,它將拋出NoSuchElementException,這將返回false,因此WebDriverWait將繼續執行,直到element存在,並且element.Displayed返回true或達到TimeOut

相關問題