Omu Omu - 6 months ago 20
Ajax Question

wait for an ajax call to complete with Selenium 2 web driver

I'm using selenium 2 web driver to test an ui which uses AJAX.

Is there a way to make the driver to wait for a bit so that the ajax request will complete

basically I have this:

d.FindElement(By.XPath("//div[8]/div[3]/div/button")).Click();
// this^ click triggers an ajax request which will fill the below Id with content
// so I need to make it wait for a bit

Assert.IsNotEmpty(d.FindElement(By.Id("Hobbies")).Text);

Answer
var wait = new WebDriverWait(d, TimeSpan.FromSeconds(5));
var element = wait.Until(driver => driver.FindElement(By.Id("Hobbies")));
Comments