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

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

问题描述

有时当我调用 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

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

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 后 Selenium 停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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