2

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 ?

2 Answers 2

2

In your catch do you still want the element?

If so then you'll want to return it return Find(Element, Text);

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but i want 'proper' element, if there's exception, then don't return it, but call again and return once there is no exception.
Since it is your catch calling itself when you have return it will return to the place where it called it, in this case it will be your own method. So effectively adding the return will return a result if you are able to recover somehow otherwise, as your method is written now it will eventually return null and that null will bubble back up to the top, returning null.
0

Maybe you must check the value of the variable "[Optional] string[] Element" that you are passing to the function. Because int the error output, it is stated that a variable in the try block is null and it cannot handle because, in the catch block the error type is "StaleElementReferenceException".

My advice is to apply one more catch block that handles Null errors and put a breakpoint there. Check which values are null and which are not then you will realize what is missing.

Sample Code:

    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);
    }
    catch (NullReferenceException e)
    {
        //break point here.
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.