硒在调用FindElements后停止工作 [英] Selenium stops to work after call FindElements

查看:105
本文介绍了硒在调用FindElements后停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,当我调用Selenium FindElements(By)时,它将引发异常,并且驱动程序停止工作.参数"BY"可能是问题所在:当我使用其他BY搜索相同的元素时,它就起作用了.

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 to search the same elements, it works.

我还可以看到,即使我的元素存在,或者以前曾使用相同参数调用过相同方法,也不会阻止该方法引发异常.

Also I could see that even if my element exists or if it this same method with the same argument was called before, it does not prevents the method to throws an exception.

我的方法是:

    public IWebElement SafeFindElement(By by)
    {
        try
        {
            IWebElement element;
            if (_driver.FindElements(by).Any())
            {
                element = _driver.FindElements(by).First();
                return element;
            }

            return null;
        }
        catch (NoSuchElementException)
        {
            return null;
        }
        catch (Exception)
        {
            return null;
        }
    }

一个BY值的示例不能始终有效(即使它存在于页面中):

An exemple of BY value that do not works all the time (even if it exists in the page):

By.CssSelector("input[data-id-selenium='entrar']")

例外:

WebDriverException

WebDriverException

对远程WebDriver服务器的URL的HTTP请求 http://localhost:46432/session/ef6cd2f1bf3ed5c924fe29d0f2c677cf/elements 60秒后超时.

The HTTP request to the remote WebDriver server for URL http://localhost:46432/session/ef6cd2f1bf3ed5c924fe29d0f2c677cf/elements timed out after 60 seconds.

我不知道这可能是什么或导致这种不稳定的原因.有人提出任何建议吗?

I have no idea what it can be or what cause this instability. There is someone with any sugestion?

@EDIT

我找到了一个临时解决方案.

I found a temporary solution.

早期,我试图使用以下元素来查找元素:

Early, I was trying to find the element using:

var element = browser
    .FindElements(By.CssSelector("input[data-id-selenium='entrar']")
    .FirstOrDefault();

var element = browser
    .FindElements(By.XPath("//input[@data-id-selenium='entrar']");
    .FirstOrDefault();

现在,我正在使用:

var element = browser
    .FindElements(By.TagName("input"))
    .FirstOrDefault(x => x.GetAttribute("data-id-selenium") == "entrar");

他们做同样的事情,但是首先会无缘无故地抛出异常.另外,这是一个临时解决方案,我正在尝试解决仅使用选择器搜索元素的问题.

They do the same thing, but the firsts throws an exception without a reason. Also, it is a temporary solution and I'm trying solve the problem to search the element only using Selectors.

推荐答案

我发现了问题.我在所有测试服中都使用一种方法来等待加载消息关闭.但是它尝试使用jquery,但并非我的应用程序中的所有页面都使用它.

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.

因此,Selenium放弃尝试在60秒后执行jquery并返回超时错误,但是此错误不会破坏Selenium驱动程序,只有FindElements会返回一个空列表.当它尝试返回空列表时,所有驱动器都坏了.

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.

原始方法:

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

以及更正:

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

这篇关于硒在调用FindElements后停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆