繁体   English   中英

在使用显式等待之前关闭隐式等待,因为显式等待需要隐式等待的时间

[英]Turning Off Implicit Wait before using explicit wait as explicit wait takes time from Implicit wait

场景:我在每次操作(单击/发送键)后寻找错误元素。

由于应用程序中的不一致,我同时使用隐式和显式等待。 初始化驱动程序时,隐式等待设置为 10 秒。 每次点击后调用下面的代码来检查页面中的任何错误元素,如果我不关闭隐式等待(通过设置为 0),显式等待仍然需要 10 秒等待(在隐式等待中给出)尽管指定等待 Object 中的 1 秒时间用于显式等待。

问题:是否可以关闭隐式等待并在所需检查后重新设置以查找错误元素

try {
    Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);

    var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(1));
    isDisp = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(errorLocator));
    Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

    Log.setTestDebug("Checking error after");
}

最佳做法是不使用隐式等待,我同意@pcalkins 的观点。

但是正如您在问题的第一部分提到的那样,由于不一致,如果您被迫使用隐式等待,则关闭并打开它们是安全的。 我用我个人的经历来回答这个问题。 我并不是说这是最好的方法或做法。

如果您打算这样做,我建议您在 finally 块中重新打开隐式等待。

try
{
  turn off implicit wait
  do error check
}
catch 
{
  exception handling
}
finally
{
  turn on implicit wait
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM