protected SelectElement GetSelectElement(By selector)
    {
        new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(c =>
        {
            try
            {
                new SelectElement(driver.FindElement(selector));
                return true;
            }
            catch (StaleElementReferenceException)
            {
                return false;
            }
        });

        return new SelectElement(driver.FindElement(selector));
    }


即使使用此功能,我仍然会在返回行上得到过时的元素,不确定是否要避免过时的元素。

最佳答案

看起来您正在执行boolean检查,但在返回SelectElement时未使用它。结果,return new SelectElement(driver.FindElement(selector));通过不关心先前所做的事情而抛出StaleElementException

protected SelectElement GetSelectElement(By selector)
{
    bool flag = new WebDriverWait(Driver, TimeSpan.FromSeconds(30)).Until(c =>
    {
        try
        {
            new SelectElement(Driver.FindElement(selector));
            return true;
        }
        catch (StaleElementReferenceException)
        {
            return false;
        }
    });

    if (flag)
    {
        return new SelectElement(Driver.FindElement(selector));

    }
    else
    {
        //something
    }

    return null;
}

10-08 05:20