如何让selenium等待日历小部件之类的内容加载?现在,我只是在将测试用例导出到junit程序后执行一次Thread.sleep(2500)
。
发布于 2010-05-14 23:15:27
我会用
waitForElementPresent(locator)
这将一直等到元素出现在DOM中。
如果需要检查元素是否可见,最好使用
waitForElementHeight(locator)
发布于 2011-02-15 15:40:50
与等待元素相比,更一般的解决方案是等待与服务器的所有连接关闭。这将允许您等待所有ajax调用完成,即使它们没有任何回调,因此不会影响页面。更多细节可以在here上找到。
使用C#和jQuery,我创建了以下方法来等待所有AJax调用完成(如果有人有更直接的方法从C#访问JS变量,请评论):
internal void WaitForAjax(int timeOut = 15)
{
var value = "";
RepeatUntil(
() => value = GetJavascriptValue("jQuery.active"),
() => value == "0",
"Ajax calls did not complete before timeout"
);
}
internal void RepeatUntil(Action repeat, Func<bool> until, string errorMessage, int timeout = 15)
{
var end = DateTime.Now + TimeSpan.FromSeconds(timeout);
var complete = false;
while (DateTime.Now < end)
{
repeat();
try
{
if (until())
{
complete = true;
break;
}
}
catch (Exception)
{ }
Thread.Sleep(500);
}
if (!complete)
throw new TimeoutException(errorMessage);
}
internal string GetJavascriptValue(string variableName)
{
var id = Guid.NewGuid().ToString();
_selenium.RunScript(String.Format(@"window.$('body').append(""<input type='text' value='""+{0}+""' id='{1}'/>"");", variableName, id));
return _selenium.GetValue(id);
}
发布于 2014-04-30 16:15:24
如果使用python,您可以使用此函数,该函数单击按钮并等待DOM更改:
def click_n_wait(driver, button, timeout=5):
source = driver.page_source
button.click()
def compare_source(driver):
try:
return source != driver.page_source
except WebDriverException:
pass
WebDriverWait(driver, timeout).until(compare_source)
(来源:基于this stack overflow answer)
https://stackoverflow.com/questions/2835179
复制相似问题