0

I'm using wait until lambda driver: driver.execute_script("return jQuery.active == 0") to make sure page loads and finished ajax call.

But when I click on one of the buttons I see XHR call going on in network log but window.jQuery shows undefined, so I can't use above command.

How can I wait until XHR call finishes or I get response.

WebDriverWait(driver, 30).until(lambda driver: driver.execute_script("return jQuery.active == 0")
user2661518
  • 2,677
  • 9
  • 42
  • 79
  • How does your js code looks like? usually when calling `$.ajax`, you have a `success` and an `error` callback to check if your call failed or works – toffler Mar 12 '19 at 16:03
  • @toffler added code – user2661518 Mar 12 '19 at 17:46
  • Could it be the xhr is sent from a js code that's not jQuery - a vanilla javascript doing it? `jQuery.active` is an internal counter which the framework increments itself, so if the call was outside of jQuery that would explain it. If that's so, you can wait for/confirm the xhr completion by the page's state change - it must "do" something for the page, like changing or adding data. – Todor Minakov Mar 13 '19 at 05:26
  • @user2661518 I've found this for selenium ajax, I hope it helps with your problem: [Handle Ajax in Selenium](https://www.toolsqa.com/selenium-cucumber-framework/handle-ajax-call-using-javascriptexecutor-in-selenium/) – toffler Mar 13 '19 at 08:05

1 Answers1

0

you can wait until xhr finished by setting ajax async: false.

To use ajax you must have jQuery library only then it will work

Example:

$.ajax({
    url : "/submit/checkuser/",
    type : "get",
    async: false,
    success : function(userStatus) {
       if (userStatus == 'Disabled') { // check if user if disabled                    
       } else if (userStatus == 'Deleted') { // check if user if deleted
       } else {
       };
    },
    error: function() {
       connectionError();
    }
 });

This ajax is asyc false so that it complete its execution and moved to the next function

For python selenium please refer the url below

webdriver wait for ajax request in python

Bathri Nathan
  • 1,101
  • 2
  • 13
  • 17