我正在使用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);
发布于 2011-06-01 21:35:44
var wait = new WebDriverWait(d, TimeSpan.FromSeconds(5));
var element = wait.Until(driver => driver.FindElement(By.Id("Hobbies")));
发布于 2011-08-26 19:12:09
如果对ajax请求使用jQuery,可以等到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);
}
}
发布于 2013-04-03 23:15:37
您还可以在这里使用Selenium显式等待。这样你就不需要自己处理超时了
public void WaitForAjax()
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait.Until(d => (bool)(d as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0"));
}
https://stackoverflow.com/questions/6201425
复制相似问题