WebDriverException when not finding elements with FIndElements

0

When I try to check whether or not an element exists on my web page, and if the element does not exist, my IWebDriver launches a WebDriverException , and all methods in my driver stop working, I continue to run the test even doing the treatment to return null or 0 in a catch.

I would like to remember the verification method I'm using is in the Selenium documentation, saying to use FindElements instead of FindElement: findElement should not be used to look for non-present elements, use WebDriver.findElements(By) and assert zero length response instead.

I'm using the latest version of Selenium WebDriver and ChromeDriver for C #.

Error reported in Exception:     [OpenQA.Selenium.WebDriverException]

The HTTP request to the remote WebDriver server for URL http://localhost:18144/session/5eeb22629a0d6f35b829113728c3f5a7/elements timed out after 60 seconds.'

Method that I use to check whether element exists or not:

public IWebElement SafeFindElement(By by)
{
    try
    {
        var element = _driver.FindElements(by);
        return element.FirstOrDefault();
    }
    catch (NoSuchElementException)
    {
        return null;
    }
    catch (Exception)
    {
        return null;
    }
}
    
asked by anonymous 02.09.2015 / 23:16

1 answer

0

I discovered the problem, so I'll share it with someone. It happened in a method that I call many times in the test suite, where it checks if the page is loading (and displaying a modal loading) and waits until it is gone. This is necessary so that the modal does not influence the tests.

However, the page I was trying to check did not have jquery, it was just a page with an html body and css displaying a certificate, there it fell into exeption that you can see below n times until completing an interval of 1 minute and then fell into the exception with an error other than $ is not defined , out of that method, and from that, FindElements would break selenium whenever it tried to return a list with 0 elements.

The method that caused this, without the error handling:

public void WaitLoadingMessage(int tempoRestanteLoading)
{
    while (tempoRestanteLoading > 0)
    {
        try
        {
            var loadingIsVisible = _js.ExecuteScript("return $('#loading-geral').is(':visible');").ToString();

            if (loadingIsVisible.ToLower() == "false")
                break;

            Thread.Sleep(1000);
            tempoRestanteLoading -= 1000;
        }
        catch (Exception ex)
        {
            if (!ex.Message.ToLower().Contains("$ is not defined"))
                throw;
        }
    }
}

The fix found for the method:

public void WaitLoadingMessage(int tempoRestanteLoading)
{
    while (tempoRestanteLoading > 0)
    {
        try
        {
            var loadingIsVisible = _js.ExecuteScript("return $('#loading-geral').is(':visible');").ToString();

            if (loadingIsVisible.ToLower() == "false")
                break;

            Thread.Sleep(1000);
            tempoRestanteLoading -= 1000;
        }
        catch (Exception ex)
        {
            if (!ex.Message.ToLower().Contains("$ is not defined"))
                throw;

            Thread.Sleep(1000);
            tempoRestanteLoading -= 1000;
        }
    }
}
    
03.09.2015 / 15:54