Selenium等待jQuery.Masonry

时间:2016-07-22 10:12:35

标签: java jquery selenium-webdriver jquery-masonry

我试图等到砌体在Java中完全加载此代码

    String javaScript = "function defer() {"
                        +"if (window.jQuery.masonry)"
                        +"{ return true }"
                        +"else"
                        +"{ return false }"
                        +"}; defer()";

    boolean isMasonryLoaded=false;
    while(!isMasonryLoaded)
    {
        isMasonryLoaded = (boolean)(((JavascriptExecutor)driver).executeScript(javaScript));
    }

但是我在while(!isMasonryLoaded)得到NullPointerException 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

可能是您的脚本函数defer()在调用之前没有定义,您应该尝试使用executeAsyncScript,如下所示: -

String javaScript = "var callback = arguments[0];"+
            "function defer() {if (window.jQuery.masonry){ "+
            "callback(true) }else{ callback(false) }}; defer()";

boolean isMasonryLoaded=false;
while(!isMasonryLoaded)
{
 isMasonryLoaded = (boolean)(((JavascriptExecutor)driver).executeAsyncScript(javaScript));
}

希望它有所帮助.. :)