【问题标题】:selenium chrome driver explicit wait is not workingselenium chrome 驱动程序显式等待不起作用
【发布时间】:2013-04-12 10:49:50
【问题描述】:

我正在使用 selenium 2 和 chrome 驱动程序,无论我做什么,似乎都无法明确等待工作。我正在尝试单击一个通过 ajax 动态生成一些数据的元素(不重新加载),然后在页面上出现该元素时搜索它。

这是我的代码

        leagueNameItem.Click();

        IList<IWebElement> outerTables_forEachLeague = new List<IWebElement>();

        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
        outerTables_forEachLeague = wait.Until<IList<IWebElement>>((d) =>
        {
            return d.FindElements(By.ClassName("boxVerde"));
        });

未找到该元素(它肯定在页面上)。等待函数实际上并没有“等待”指定的 10 秒 ut 只是什么都不返回。有什么想法吗?

【问题讨论】:

  • 是的,他们会在等待 1 或 2 秒的情况下被发现...没有其他问题,因为我已经尝试过了...只是等待不起作用...
  • 我会假设,因为.FindElements 如果找不到任何东西,它会返回一个空列表,它只是返回。如果你把它设为.FindElement,这会返回something吗?

标签: c# selenium webdriver


【解决方案1】:

问题是FindElements 立即返回,如果未找到元素,则返回一个有效的空列表对象。你有两个选择。您可以在等待中使用单个 FindElement,如果该元素不存在,则会引发异常。 WebDriverWait 对象将捕获该异常并重试,直到找到该元素。

但是,由于您想从等待中返回一个列表,因此您需要更聪明一点,这会导致您的第二个选择。将您的等待更改为如下所示:

leagueNameItem.Click();

IList<IWebElement> outerTables_forEachLeague = new List<IWebElement>();

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
outerTables_forEachLeague = wait.Until<IList<IWebElement>>((d) =>
{
    var elements = d.FindElements(By.ClassName("boxVerde"));
    if (elements.Count == 0)
    {
        return null;
    }

    return elements;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多