Selenium stops to work after call FindElements

后端 未结 2 1389
花落未央
花落未央 2021-01-28 02:26

Sometimes when I call the Selenium FindElements(By), it throws an exception and my driver stops to work. The parameter \"BY\" maybe can be the problem: when I use a different by

2条回答
  •  一整个雨季
    2021-01-28 03:00

    I found the problem. I'm using a method in all my test suit to wait the loading message dismiss. But it try using jquery and not all pages in my application use it.

    So, selenium give up try execute jquery after 60 seconds and returns a timeout error, but this error does not broken the Selenium driver, only the FindElements then it returns a empty list. And when it try to return the empty list, all the drive broke.

    The original method:

    public void WaitLoadingMessage(int timeout)
    {
        while (timeout > 0)
        {
            try
            {
                var loadingIsVisible = _js.ExecuteScript("return $('#loading-geral').is(':visible');").ToString();
    
                if (loadingIsVisible.ToLower() == "false")
                    break;
    
                Thread.Sleep(1000);
                timeout -= 1000;
            }
            catch (Exception ex)
            {
                if (!ex.Message.ToLower().Contains("$ is not defined"))
                    throw;
            }
        }
    }
    

    And the correction:

    public void WaitLoadingMessage(int timeout)
    {
        while (timeout > 0)
        {
            try
            {
                var loadingIsVisible = _js.ExecuteScript("return $('#loading-geral').is(':visible');").ToString();
    
                if (loadingIsVisible.ToLower() == "false")
                    break;
    
                Thread.Sleep(1000);
                timeout -= 1000;
            }
            catch (Exception ex)
            {
                if (!ex.Message.ToLower().Contains("$ is not defined"))
                    throw;
    
                break;
            }
        }
    }
    

提交回复
热议问题