我编写了一个 IsElementPresent 方法,它返回 true/false 是否显示元素。
这是我的方法
public static bool IsElementPresent(this IWebElement element)
{
try
{
return element.Displayed;
}
catch (Exception)
{
return false;
}
}
现在有时当它应该返回 false 时,element.Displayed 在捕获异常并返回 false 之前等待大约 20 秒(通过调试找到)。如果它找到元素,它工作正常。
我还将代码更改为:
public static bool IsElementPresent(this IWebElement element)
{
try
{
WebDriverWait wait = new WebDriverWait(DriverContext.Driver, TimeSpan.FromSeconds(0.1));
wait.Until(ExpectedConditions.ElementToBeClickable(element));
return true;
}
catch (Exception)
{
return false;
}
}
现在同样的等待发生在 Wait.Until 行。代码工作正常,但在找不到元素时只是不必要的延迟。如何找到元素是否重要。当按类找到元素时,就会发生这种特殊的延迟。大多数其他元素是使用 xpath、css 或 id 找到的。如果我错过任何信息,请告诉我。使用 VS 社区 15.5.6