How can I ask Selenium WebDriver to wait a while?

I can write the following code to ask WebDriver to wait someday

new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.id("loginBox"))); 

But,

In fact, I am sending an AJAX request to the server. Here I gave 20 milliseconds to wait. 20 ms 500 ms does not matter. If the response exceeds the specified time (for example, 20 ms). Then I will exclude that No Such Element was not found.

So, is there a better way to ask the server to wait?

Can anybody help me?

Thanks in advance, Gnik

+4
source share
2 answers

You can verify that the employeeName text field is filled in before proceeding with this code -

 new WebDriverWait(driver,10).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { String text = driver.findElement(By.id("employeeName")).getText(); return !text.equals(""); } }); 

Now this code checks to see if the text in the employeeName text field is empty. If it is empty, the driver waits 10 seconds or if some data is filled in the text field due to an AJAX call, then it continues with execution.

If this does not work, you can publish part of your code with which you make an AJAX call.

+5
source
 public void WaitForAjaxCompletion() { Stopwatch sw = new Stopwatch(); sw.Start(); try { while (sw.Elapsed.TotalSeconds < int.Parse(ConfigurationManager.AppSettings["TimeOut"])) { var ajaxIsComplete = (bool)((IJavaScriptExecutor)_webDriver).ExecuteScript("return window.jQuery != undefined && jQuery.active==0"); if (ajaxIsComplete) break; Thread.Sleep(100); } } catch (Exception) { Console.WriteLine("Ajax call time out time {0} has passed ", ConfigurationManager.AppSettings["TimeOut"].ToString()); } finally { sw.Stop(); } } 
0
source

All Articles