Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
184 views
by The go-to Tester (158 points)
retagged by
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++)

    {

         try{

            driver.FindElement(By by);

            return true;

        }

        catch(NotFoundElementException){

        }

    Thread.Sleep(1000);

    }

 return false;

}

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

1 Answer

1 like 0 dislike
by The go-to Tester (181 points)
selected by
 
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.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...