Reputation: 21
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
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