Site Loader
Auckland, New Zealand
There are many situations where we might need to wait for page to fully load before performing any operations in the UI control of the page using Selenium, but in modern application we might not just have to wait for the page to load but also the behind the scenes operation, which is nothing but the Asynchronous Java script (Ajax) calls to complete (Which performs data binding of control once it gets the response from the data source, it happens even after page load). Our test will fail tentatively due to these situations. Hence its always wise idea to wait for Ajax call to complete. This can be done using our IJavaScriptExecutor interface, which we already discussed in our previous posts. The idea is simple, if all the Jquery executions are completed, then it will return == 0 which we can use in our Wait.Until method to wait till the script return as true. Hence we can write the code as shown in the below snippet
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(20));
IJavaScriptExecutor jsScript = Driver as IJavaScriptExecutor;
wait.Until((d) => (bool)jsScript.ExecuteScript("return == 0"));
As shown in the above C# code snippet, we are first creating an instance of WebDriverWait class and then creating an instance of IJavaScriptExecutor class and using the ExecuteScript method we are waiting Until the == 0 returns true. This way we can wait for all the Ajax calls to complete in a page using Selenium This method will come in handy in many situations. Thanks, Karthik KK

Post Author: Karthik kk

One Reply to “How to wait for all the Ajax request to complete in a page using Selenium”

Leave a Reply

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