Webdriverexception when not finding elements with Findelements

Asked

Viewed 1,033 times

0

When I try to check if an element exists or not on my website and if the element does not exist, my Iwebdriver launches a Webdriverexception, and all of my driver’s methods stop working, making it impossible for me to continue with the test running even while doing the treatment to return null or 0 in a catch.

I would like to remind you 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 I use to verify whether or not an element exists:

public IWebElement SafeFindElement(By by)
{
    try
    {
        var element = _driver.FindElements(by);
        return element.FirstOrDefault();
    }
    catch (NoSuchElementException)
    {
        return null;
    }
    catch (Exception)
    {
        return null;
    }
}

1 answer

0


I ended up discovering the problem, so I’ll share it with someone else. It happened in a method that I often call in the test suite, where it checks if the page is loading (and displaying a loading modal), and waits until it is no longer. This is necessary so that the modal does not influence the tests.

However, the page that I was trying to do this check had no jquery, it was just a page with a body in html and css displaying a certificate, then it fell into the exeption that you can see below n times until you complete a 1 minute interval happening this, and then fell into Exception with a mistake other than $ is not defined, then left the method and, from that, Findelements broke Selenium whenever he tried to return a list with 0 elements.

The method that caused this, without the error treatment:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.