Java Selenium 等待 Ajax 内容加载 - 通用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33348600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium wait for Ajax content to load - universal approach
提问by Fabian Lurz
Is there a universal approach for Selenium to wait till all ajax content has loaded? (not tied to a specific website - so it works for every ajax website)
Selenium 是否有一种通用的方法可以等到所有 ajax 内容加载完毕?(不绑定到特定网站 - 所以它适用于每个 ajax 网站)
采纳答案by LINGS
You need to wait for Javascript and jQuery to finish loading. Execute Javascript to check if jQuery.active
is 0
and document.readyState
is complete
, which means the JS and jQuery load is complete.
您需要等待 Javascript 和 jQuery 完成加载。执行 Javascript 来检查jQuery.active
is0
和document.readyState
is complete
,这意味着 JS 和 jQuery 加载完成。
public boolean waitForJSandJQueryToLoad() {
WebDriverWait wait = new WebDriverWait(driver, 30);
// wait for jQuery to load
ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
try {
return ((Long)((JavascriptExecutor)getDriver()).executeScript("return jQuery.active") == 0);
}
catch (Exception e) {
// no jQuery present
return true;
}
}
};
// wait for Javascript to load
ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor)getDriver()).executeScript("return document.readyState")
.toString().equals("complete");
}
};
return wait.until(jQueryLoad) && wait.until(jsLoad);
}
回答by Slav Kurochkin
As Mark Collin described in his book "Mastering Selenium Webdriver", use JavascriptExecutor let you figure out whether a website using jQuery has finished making AJAX calls
正如 Mark Collin 在他的《掌握 Selenium Webdriver》一书中所描述的,使用 JavascriptExecutor 可以让你判断一个使用 jQuery 的网站是否已经完成了 AJAX 调用
public class AdditionalConditions {
public static ExpectedCondition<Boolean> jQueryAJAXCallsHaveCompleted() {
return new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return (Boolean) ((JavascriptExecutor) driver).executeScript("return (window.jQuery != null) && (jQuery.active === 0);");
}
};
}
}
回答by eagle12
I don't believe that there is a universal approach out of the box. I typically make a method that does a .waituntilrowcount(2)
or waituntilvisible()
that polls an element.
我不相信有一种开箱即用的通用方法。我通常会创建一个方法来执行.waituntilrowcount(2)
或waituntilvisible()
轮询元素。
回答by Aniket Wadkar
I have been using this simple do while to iterate until an AJAX is finished. It consistently works for me.
我一直在使用这个简单的 do while 进行迭代,直到 AJAX 完成。它始终对我有用。
public void waitForAjax() throws InterruptedException{
while (true)
{
Boolean ajaxIsComplete = (Boolean) ((JavascriptExecutor)driver).executeScript("return jQuery.active == 0");
if (ajaxIsComplete){
info("Ajax Call completed. ");
break;
}
Thread.sleep(150);
}
}