I have Ajax events on my website. For exampe, when I click button one of the elements will change his state. But only when response will come from server.

Thread.Sleep(5000) is bad idea. I wrote this method:

public bool IsElementApear(By by){

    for (int i = 0; i < 10 i++)



            driver.FindElement(By by);

            return true;






 return false;


Is there a better way to wait certain state?
by (171 points)
When you say change state or certain state, do you mean enabled/disable or visible or invisible?
by (158 points)
I need to wait until the item appears in the DOM.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

by (171 points)
selected by
+1 vote
Best answer

Have you tried webdriverwait?

public static Func<IWebDriver, IWebElement> ElementIsClickable(By locator)
    return driver =>
        var element = driver.FindElement(locator);
        return (element != null && element.Displayed && element.Enabled) ? element : null;
User above function something like this:
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementIsClickable

Hope that helps.