【问题标题】:Selenium with NUnit and .Net giving NoSuchElementExcpectionSelenium 与 NUnit 和 .Net 提供 NoSuchElementExcpection
【发布时间】:2012-05-19 09:59:09
【问题描述】:

我正在使用 selenium 进行自动化测试。当测试在 Selenium IDE 上运行时,它会成功运行,当将其导出为 .Net Web 驱动程序并使用 NUnit 运行时,会抛出 NoSuchElementsException。请问有人是什么导致了这个异常吗?

SeleniumTests.UserNotLogged.TheUserNotLoggedTest:

OpenQA.Selenium.NoSuchElementException : Unable to locate element: {"method":"link text","selector":""} //异常来自 NUnit

[Test]
    public void TheUserNotLoggedTest()
    {
        driver.Navigate().GoToUrl(baseURL + "Index.aspx");
        driver.FindElement(By.Id("wrapper")).Click();
        driver.FindElement(By.LinkText("Home")).Click();
        driver.FindElement(By.LinkText("News")).Click(); //Exception thrown here
        driver.FindElement(By.LinkText("Events")).Click();
        driver.FindElement(By.LinkText("Contact Us")).Click();
        driver.FindElement(By.LinkText("Register")).Click();
    }

感谢您的帮助

【问题讨论】:

    标签: c# firefox selenium nunit


    【解决方案1】:

    在这种情况下,通常是时间问题。我认为这些是导航链接?在这种情况下,Selenium 很可能在页面完成加载之前就试图找到新闻链接。尝试添加一个 WebDriverWait:

    http://seleniumhq.org/docs/04_webdriver_advanced.html

    如果仍然无法解决问题,请使用其他方法来查找链接,例如 css 选择器或 XPath - 只是为了验证您是否可以使用其他方法找到它(尽管不是最好的方法)。

    【讨论】:

    • 感谢帮助,还是抛出异常
    • 我假设您无需点击“新闻”即可进入“活动”-因此请删除单击“新闻”链接-它会在“活动”上失败吗?如果是这样,那肯定是时间问题。尝试使用另一种方法,链接上是否有类?至少,尝试使用 XPath,例如:driver.FindElement(By.XPath("//a[text()='News']");
    • 我有另一个页面包含这个 driver.FindElement(By.Id("drpList")).Click();再次抛出异常,可以用 XPath 完成下拉列表吗?
    • 当然,下拉列表通常是“选择”元素,下拉列表中的每个选项都是一个“选项”元素,因此选择实际下拉列表的 XPath 将是,假设“drpList”是下拉列表:“//select[@id='drpList']”有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    相关资源
    最近更新 更多