Selenium Webdrvier – Wait jQuery Ajax Request Complete in Python

By | 15/07/2015

if you have a jQuery Ajax request in your test web page, you should wait to request to complete. Otherwise, unexpected behaviours can occur. in Selenium Webdriver, we can perform by Javascript running function: execute_script()
jQuery provide a parameter to check request status. “active” is one of jQuery library. if value is 1 it means request still in progress, if it is 0(zero) it means that it is done.

so we check by this code: == 0

you can see complete code at below in Python:

# -*- coding: UTF-8 -*-
import unittest
import datetime
from selenium import webdriver
from import WebDriverWait

class TestClass(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
    def test_MahsumAkbasNet_Pass(self):
        wait = WebDriverWait(self.driver, 30)
        print "Button is clicked at time: " + str("%Y%m%d%H%M%S"))
        wait.until(lambda driver: self.driver.execute_script("return == 0"))
        print "Ajax request is completed at time: " + str("%Y%m%d%H%M%S"))
    def tearDown(self):

if __name__ == '__main__':

you can see output, it waits about 11 seconds(requested page coded to sleep 11 seconds):

Button is clicked at time: 20150915212337
Ajax request is completed at time: 20150915212348
Ran 1 test in 22.413s


Leave a Reply

Your email address will not be published. Required fields are marked *