Selenium wait for Ajax content to load - universal approach

前端 未结 4 2046
清歌不尽
清歌不尽 2020-11-29 02:49

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)

4条回答
  •  离开以前
    2020-11-29 03:33

    I have been using this simple do while to iterate until an AJAX is finished. It consistently works for me.

    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);
        }
    }
    

提交回复
热议问题