Akif Tahir
Akif Tahir

Reputation: 171

Element not found in the cache - perhaps the page has changed since it was looked up c#

I was advised to try to use 'StaleElementReferenceException' to handle this, but am not sure how to incorporate it. If someone could provide some hints that would be much appreciated. Thank you

    [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();
            }
        }
    }

Upvotes: 1

Views: 643

Answers (1)

Mesut GUNES
Mesut GUNES

Reputation: 7411

You should add break; if the value is found it clicks and then it changes the dom which cause a problem for the next iteration.

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

Upvotes: 3

Related Questions