如何等待网络元素消失
本文关键字:网络 元素 消失 等待 何等待 | 更新日期: 2023-09-27 18:35:12
我正在测试一个应用程序,该应用程序有一个页面,上面显示了许多不同的水果。我需要按重量缩小它们的范围。提交权重范围会触发 AJAX 调用,该调用会弹出一个覆盖整个页面的加载指示器,直到调用成功返回。这是我失败的代码:
public MyPageObject InputWeightRange( int weight, int range = 1000 )
{
var page = PageFactory.InitElements<MyPageObject>(Driver);
var wait = new WebDriverWait( Driver, TimeSpan.FromSeconds( 10 ) );
wait.Until( driver => page._loadingIndicator.Displayed.Equals( false ) );
_upperWeight
.SendKeys( ( weight + ( range/2 ) ).ToString( CultureInfo.InvariantCulture ) ); //CultureInfo declarations to keep ReSharper happy.
_lowerWeight.SendKeys( ( weight- ( range/2 ) ).ToString( CultureInfo.InvariantCulture ) );
wait.Until(driver => page._loadingIndicator.Displayed.Equals(false));
wait.Until( driver => page._submitButton.Enabled );
_submitButton.Click();
wait.Until(driver => page._loadingIndicator.Displayed.Equals(false));
return this;
}
通常,代码有效。有时它在点击时失败,我想是因为加载指示器也是由我在此之前在页面上执行的其他操作触发的。如您所见,我正在使用WebDriverWait,但由于某种原因它不起作用。我从未在手动测试中看到加载指示器出现超过半秒。
实际的例外是:
System.InvalidOperationException was unhandled by user code
Message=unknown error: Element is not clickable at point (1234, 400). Other element would receive the click: <div id="loadingIndicator" class="loadingIndicator" style="display: block;">...</div>
编辑:请注意,该元素始终存在于 DOM 中,因此等待启用只会导致 WebDriverTimeout 异常。除了用try/catches或thread.sleeps乱扔我的代码之外,我如何安全地测试此页面免受异常的影响?
如果加载指示器具有任何高度/宽度属性,请尝试等待它为零或未覆盖整个页面时的尺寸