user123366
user123366

Reputation: 21

selenium stale element c#

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

Even with this function I still get stale element on the return line, not sure what else to do to avoid the stale element.

Upvotes: 2

Views: 1718

Answers (1)

Saifur
Saifur

Reputation: 16201

Looks like you are doing a boolean check but not using it when returning the SelectElement. As a result, return new SelectElement(driver.FindElement(selector)); throws StaleElementException by not caring what you have done earlier.

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

Upvotes: 1

Related Questions