Selenium WebDriver makes it easy to write automation tests for your web applications, but with lots of apps using asynchronous means of loading data, it can be hard to determine when a certain call is finished.
The code below shows how to wait until all ajax calls are complete by calling out to jQuery.active via webdriver’s execute_script method.
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import WebDriverException def ajax_complete(driver): try: return 0 == driver.execute_script("return jQuery.active") except WebDriverException: pass def my_automation_test(): ff_driver = webdriver.Firefox() ff_driver.get("http://domain.tld") #wait for ajax items to load WebDriverWait(ff_driver, 10).until( ajax_complete, "Timeout waiting for page to load") assert "ajax loaded string" in ff_driver.page_source