I think I'm really missing something here, but can't figure out what. I want this method to return a web element, but if there is this specific error, I want it to try again:
int findCounter = 0;
public IWebElement Find([Optional] string[] Element, [Optional] string Text)
{
IWebElement element;
element = null;
if (findCounter < 30){
if (Text != null)
{
Wait(null, Text);
element = driver.FindElement(By.LinkText(Text));
findCounter = 0;
return element;
}
else
{
try
{
if (Element[1] == "xpath")
{
Wait(Element, null);
element = driver.FindElement(By.XPath(Element[0]));
findCounter = 0;
}
else if (Element[1] == "id")
{
Wait(Element, null);
element = driver.FindElement(By.Id(Element[0]));
findCounter = 0;
}
else if (Element[1] == "linktext")
{
Wait(Element, null);
element = driver.FindElement(By.LinkText(Element[0]));
findCounter = 0;
}
}
catch (StaleElementReferenceException e)
{
Console.Out.WriteLine("Attempting to recover from StaleElementReferenceException ...");
Sleep(250);
findCounter++;
Find(Element, Text);
}
return element;
}
} return null;
}
So i try it:
Find(Element, null)
And it gets to error, outputs message, then fails:
Attempting to recover from StaleElementReferenceException ...
NullReferenceException 'Object reference not set to an instance of an object.'
But i use same parameters as first time. What could be wrong ?