How to prevent StaleElementReferenceException in PageFactory in Selenium C#?

独自空忆成欢 提交于 2019-12-22 13:52:20

问题


I have a Selenium WebDriver Page Object Model in C# 6. I am experiencing StaleElementReferenceExceptions while trying to click on ajax-loaded nav bar elements. This is confusing, because I'm using PageFactory, without any [CacheLookup]s.

Here's the code in question. I've tried to simplify to just the important parts. (I actually pass around a Driver, a wrapper around IWebDriver.) MenuBar.SelectEnglish<T>() throws the exception.

public class Tests
{
    [Test]
    public void SelectEnglishTest()
    {
        homePage
            .MenuBar.SelectEnglish<HomePage>();
    }

    // ...
}

public class MenuBar : PageObject
{
    [FindsBy(How = How.CssSelector, Using = "...")]
    private IWebElement Language { get; set; }

    [FindsBy(How = How.CssSelector, Using = "...")]
    private IWebElement English { get; set; }

    public T SelectEnglish<T>() where T : Page
    {
        Language.Click();
        IWait<IWebDriver> wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(5));
        wait.Until(ExpectedConditions.ElementToBeClickable(English));
        English.Click();
        Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
        return (T)Activator.CreateInstance(typeof(T), Driver);
    }

    // ...
}

public class HomePage : PageObject
{
    public MenuBar MenuBar { get; private set; }

    // ...
}

public class PageObject
{
    protected IWebDriver Driver { get; }

    protected PageObject(IWebDriver driver)
    {
        Driver = driver;
        PageFactory.InitElements(this, new RetryingElementLocator(Driver, TimeSpan.FromSeconds(20)));
    }

    // ...
}

What is causing this error? What can I do about it?


回答1:


From the docs, Stale Element Reference Exception

A stale element reference exception is thrown in one of two cases, the first being more common than the second:
 - The element has been deleted entirely.
 - The element is no longer attached to the DOM.

Since you mentioned the element is loaded using Ajax, most likely the element changed after your page object fetched it. Just fetch it again or wait for the Ajax to complete before fetching the affected elements.


EDIT 1

Here's some sample code to show how you can fetch an element using a method even while using PageFactory.

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.PageObjects;
using System;

namespace C_Sharp_Selenium_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            FirefoxDriver driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://www.google.com");
            HomePage homePage = new HomePage(driver);
            PageFactory.InitElements(driver, homePage);
            homePage.search("stack overflow");
            homePage.getSearchBox().Clear();
            homePage.getSearchBox().SendKeys("c# pagefactory");
            homePage.getSearchButton().Click();
        }
    }

    public class HomePage
    {
        private By searchBox = By.Id("lst-ib");
        private By searchButton = By.Name("btnG");

        // add other elements in here that use FindsBy() to be loaded using PageFactory.InitElements()

        private IWebDriver driver;

        public void search(String s)
        {
            getSearchBox().SendKeys(s);
            getSearchButton().Click();
        }

        public IWebElement getSearchBox()
        {
            return driver.FindElement(searchBox);
        }

        public IWebElement getSearchButton()
        {
            return driver.FindElement(searchButton);
        }

        public HomePage(IWebDriver driver)
        {
            this.driver = driver;
        }
    }
}


来源:https://stackoverflow.com/questions/32209199/how-to-prevent-staleelementreferenceexception-in-pagefactory-in-selenium-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!