在缓存中找不到元素 - 也许页面自查找以来已更改c#

时间:2015-10-15 11:06:50

标签: c# .net selenium selenium-webdriver

我被建议尝试使用' StaleElementReferenceException'处理这个问题,但我不确定如何加入它。如果有人可以提供一些非常感激的提示。谢谢

    [Then(@"I select the following list item '(.*)' from my search")]
    public static void PreSelectedListOptions(string value)
    {
        var suggestedList = Driver.Instance.FindElements(By.CssSelector(".list-reset li"));
        foreach (IWebElement suggestion in suggestedList)
        {
            if (value.Equals(suggestion.Text))
            {
                suggestion.Click();
            }
        }
    }

1 个答案:

答案 0 :(得分:3)

如果找到值,则应添加break;,然后更改dom,这会导致下一次迭代出现问题。

[Then(@"I select the following list item '(.*)' from my search")]
public static void PreSelectedListOptions(string value)
{
    var suggestedList = Driver.Instance.FindElements(By.CssSelector(".list-reset li"));
    foreach (IWebElement suggestion in suggestedList)
    {
        if (value.Equals(suggestion.Text))
        {
            suggestion.Click();
            break;
        }
    }
}