我一直致力于使我的 Selenium 框架成为页面工厂,但是我正在努力让 Wait.Until 命令在我的扩展类中工作。
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));
}
Run Code Online (Sandbox Code Playgroud)
如果我使用上面的代码,我会收到错误无法从 OpenQA.Selenium.IWebElement 转换为 Open.Qa.Selenium.By
任何建议我如何修改上面的代码以使其在我使用的 By 模型中工作?
没有ExpectedConditions.ElementIsVisible(IWebElement)
。不幸的是,你只能使用ElementIsVisible
与By
对象。
如果合适,您可以替换为ExpectedConditions.ElementToBeClickable(IWebElement)
,这是一种稍微不同的情况,除了可见之外,它还检查元素是否已启用。但这可能满足您的要求。
或者,您可以只调用element.Displayed
custom 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;
}
}
Run Code Online (Sandbox Code Playgroud)
上面代码的快速解释......它会尝试element.Displayed
一遍又一遍地执行,直到它返回true
。当element
不存在时,它将抛出一个NoSuchElementException
将返回的 ,false
因此WebDriverWait
将继续执行,直到两者都element
存在,并element.Displayed
返回真,或TimeOut
达到。
归档时间: |
|
查看次数: |
4656 次 |
最近记录: |