0

What is the best way to use I have been getting Timeout excption and below is the code I am using to FindElement.

if i use Thread.Sleep(8000) or 6000 its working as excepted but Its scattered all over my code and its hard to maintain ... is there any elegant solution for this problem?

    public IWebElement GetFindElement(By locator)
    {
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
        IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
        {
            return d.FindElement(locator);
        });
        return myDynamicElement;
    }

Error:

[System.TimeoutException] = {"Timed out after 20 seconds"}

1 Answer 1

1

Try to avoid thread.sleep constructions. I can recommend use Implicit waits

protected static void ImplicitlyWait(int timeToWait)
    {
        _driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(timeToWait));
    }
Sign up to request clarification or add additional context in comments.

4 Comments

But under the covers ImplicitlyWait is simply calling Thread.sleep(200) anyway.
ImplicitlyWait waits for elements to show up while threading just waits constant time.
@vlad: I was reading this thread and found that its good idea to introduce have a look this pls groups.google.com/forum/m/?fromgroups#!topic/webdriver/…
You mean timers? I haven't work with them, so I'm not able to say is it good or not. Try it at practice, it might be better solution in your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.