不使用PageFactory时出现NoSuchElementException

时间:2019-10-15 13:28:58

标签: c# selenium-webdriver page-factory

由于在C#的最新硒版本中不推荐使用PageFactory,所以我试图重新编写现有的PageObject,如下面的代码片段所示。除了元素尚未加载之外,其他所有操作都像以前一样。在与它们进行交互之前,我已经编写了ExplicitWaitConditions,但是在到达ExplicitWaitConditions之前,我在FindElement(By.Id(“ email”))本身上遇到了NoSuchElementException异常。当我使用PageFactory时,它工作正常。 非常感谢您的帮助和建议

        private IWebDriver driver;
        public LoginPage(IWebDriver _driver)
        {
            driver = _driver;
            //if (driver != null)
            //{
            //    PageFactory.InitElements(driver, this);
            //}
        }

        //[FindsBy(How = How.Id, Using = "email")]
        //public IWebElement txtUserName { get; set; }

        //No such element exception is thrown by below line
        public IWebElement txtUserName => driver.FindElement(By.Id("email"));

基本上,我想了解的是如何在不使用PageFactory的情况下实现延迟初始化。由于错误甚至是在元素访问之前就抛出的。

1 个答案:

答案 0 :(得分:0)

尝试如下添加“ this”

  private IWebDriver _driver;
    public LoginPage(IWebDriver _driver)
    {
        this._driver = _driver;
    }

private IWebElement FooElement
    {
        get
        {
            return this._driver.FindElement(By.Id("foo"));
        }
    }