【问题标题】:FindElements not found causes WebDriverExceptionFindElements not found 导致 WebDriverException
【发布时间】:2014-12-17 11:43:57
【问题描述】:

尝试在页面上运行以下 FindElements 方法时:

var match = Driver.Instance.FindElements(By.LinkText("Click here"));

我收到错误消息:

An exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll but was not handled in user code.
OpenQA.Selenium.WebDriverException was unhandled by user code
HResult=-2146233088
Message=The HTTP request to the remote WebDriver server for URL http://localhost:7057/hub/session/a90c4828-3fb3-46d1-923d-8c5cbb65c4fe/elements timed out after 60 seconds.
Source=WebDriver

链接文本“单击此处”实际上并不存在于页面中,因此我不希望 FindElements(By) 实际找到任何内容(我稍后会在 If 语句中使用它)。该方法超时导致上述异常。

根据我的理解,如果 FindElements 超时,并且实际上没有找到任何东西,它应该返回 0 个元素。不只是超时并抛出异常。

有没有其他人遇到过这种情况或有任何想法可能是什么原因造成的?

【问题讨论】:

  • xander,据我所知,findElements 返回符合条件的元素集合。它可以是零个或多个元素。我希望问题是将集合分配给“var match”。你能有一个像 List eles=your findements 这样的声明并试一试吗
  • 什么版本的 Selenium,什么浏览器以及那个浏览器的什么版本?

标签: c# firefox selenium selenium-webdriver


【解决方案1】:

FindElements() 会抛出 WebDriverExepction,因为我是如何创建驱动程序的。

我已经在它自己的类中创建了驱动程序:

public class Driver
{
    public static IWebDriver Instance { get; private set; }

    public static void Initialise()
    {
        Instance = new FirefoxDriver();
        Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
    }
}

这意味着在测试用例开始时我会调用 Driver.Initialise() 然后像这样使用 Driver:

Driver.Instance.FindElements(By.Id("ABC"));

由于某种原因(我仍然不知道真正的答案),Driver 类不是静态的并且调用 FindElements 方法将返回 WebDriverException 而不是 0 个元素的列表。

只需将类更改为:

public static class Driver
{
    public static IWebDriver Instance { get; private set; }

    public static void Initialise()
    {
        Instance = new FirefoxDriver();
        Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
    }
}

解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2018-12-27
    • 2018-07-26
    相关资源
    最近更新 更多