我正在使用 Selenium 2 WebDriver 来测试使用 AJAX 的 UI。
有没有办法让驱动程序等待 Ajax 请求完成。
基本上我有这个:
d.FindElement(By.XPath("//div[8]/div[3]/div/button")).Click();
// This click trigger an ajax request which will fill the below ID with content.
// So I need to make it wait for a bit.
Assert.IsNotEmpty(d.FindElement(By.Id("Hobbies")).Text);
最佳答案
如果您使用 jQuery 来处理 ajax 请求,则可以等到 jQuery.active
属性为零。其他库可能有类似的选项。
public void WaitForAjax()
{
while (true) // Handle timeout somewhere
{
var ajaxIsComplete = (bool)(driver as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0");
if (ajaxIsComplete)
break;
Thread.Sleep(100);
}
}
关于c# - 使用 Selenium 2 WebDriver 等待 Ajax 调用完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6201425/