0

I have to click a certain button on a page. However, when I retrieve all of the elements that have a particular class name. All of the retrieved elements throw a stale reference exception when I try to perform each one or click. I can not double click on any of them. It find's the right elements but throws the exception for all of them. The commented out code is where I actually am trying to select and click the appropriate button. I attached a picture of the form. Note that the pages are changed each time a button is clicked or performed. The Select Upload BOM button is what you need to pay particular attention to. Website

 // Switch to correct frame
        IWebElement editorFrame = driver.FindElement(By.ClassName("frame-banner"));
        driver.SwitchTo().Frame(editorFrame);
        var action = new OpenQA.Selenium.Interactions.Actions(driver);
        // Select Project File 
        IList<IWebElement> projectFileButtonList= driver.FindElements(By.ClassName("data-cell"));
        foreach (var button in projectFileButtonList)
        {
            if (button.Text == "BOM_scrub")
            {
                // Found Project File now select it
                action.DoubleClick(button);
                action.Perform();
                break;
            }
        }
        // Select Upload BOM Button
        IList<IWebElement> uploadBomBtn = driver.FindElements(By.ClassName("se-custom-main-button"));
        foreach (var element in uploadBomBtn )
        {
            try
            {
                action.DoubleClick(element);
                action.Perform();
            }
            catch
            {

            }
            /*
            if (element.Text == "Upload BOM")
            {
                int i = 0;
                while (i == 0)
                {
                    try
                    {
                        action.DoubleClick(element);
                        action.Perform();
                        break;
                    }
                    catch
                    {

                    }
                }

            }
            */
        }
weknowgp
  • 49
  • 1
  • 5
  • https://stackoverflow.com/questions/45434381/stale-object-reference-while-navigation-using-selenium/45435158#45435158 – santhosh kumar Sep 02 '17 at 02:35
  • 1
    Possible duplicate of [StaleElementReference Exception in PageFactory](https://stackoverflow.com/questions/44838538/staleelementreference-exception-in-pagefactory) – undetected Selenium Sep 02 '17 at 07:50

1 Answers1

0

Don't use driver.findElement(-s) with dynamic components.

StaleElementReferenceException occurs, as you're trying to perform an action against element, which has already been detached from DOM.

You have to use explicit waits mechanism (a combination of WebDriverWait + ExpectedConditions), which automatically refreshes element's state, and returns its valid representation, when specified condition is met.

Serhii Korol
  • 843
  • 7
  • 15
  • Can you give me an example of this? I can't find WebDriverWait, I only have WebDriverException and WebDriverTimeOutException. – weknowgp Sep 05 '17 at 12:35
  • @weknowgp see https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_WebDriverWait.htm and official docs http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp with examples. – Serhii Korol Sep 05 '17 at 12:40
  • wait.Until(drv => drv.FindElements(By.ClassName("se-custom-main-button"))[9]).Click(); – weknowgp Sep 05 '17 at 13:26
  • This works fine. However, sometimes it loads too fast and begins to run this code right away. In doing so, it can't find any classes by that name and stops. If I put a wait 2 seconds before this line then it works because the program had time to actually go to the next page. Any suggestions? – weknowgp Sep 05 '17 at 13:27
  • @weknowgp seems like `WebDriverWait` misusing. You may want to check https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_ExpectedConditions.htm which should be used together with `WebDriverWait`. – Serhii Korol Sep 05 '17 at 13:32