使用WebDriver的System.TimeoutException

时间:2012-09-25 23:45:16

标签: selenium webdriver selenium-webdriver

最好的使用方法我一直在使用Timeout excption,下面是我用于FindElement的代码。

如果我使用Thread.Sleep(8000)或6000作为例外工作,但它分散在我的代码中并且难以维护......对于这个问题有没有优雅的解决方案?

    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;
    }

错误:

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

1 个答案:

答案 0 :(得分:1)

尽量避免使用thread.sleep结构。我建议使用隐式等待

protected static void ImplicitlyWait(int timeToWait)
    {
        _driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(timeToWait));
    }