Selenium C# - StaleElementReferenceException - Even after refreshing the page

327 views Asked by At

I am getting OpenQA.Selenium.StaleElementReferenceException which is a bit strange since I am refreshing the page. So here is the detail:

The technology we are using is React. Browser: Chrome ( latest version) C# with Selenium Webdriver (3.X)

We have a left menu with Li items. On first time page load i can click the left menu item using xpath and it works fine. The left menu adjusts itself so that the item no longer appears inside it. I think this updates the DOM. Ok now when i try to click the next item , i get the stale reference exception. So I did following things: 1- Refresh the page and double/single click - does not work 2- Navigate to another page and come back to this page and double/single click-- does not work 3- Added thread.sleep - does not work 4- Catch the exception and try to find the element again -- does not work.

Is there something i am missng? Here is the code:

    Actions actions = new Actions(driver);
            string[] ItemNames = { "OrganizationDepartment","Organization"};
            foreach (String ItemName in ItemNames)
                        {
                            try
                            {
                                driver.Navigate().Refresh();
        //Thread.Sleep(3000);
                            IWebElement source = driver.FindElement(By.XPath("//div[contains(text(),'" + ItemName+ "')]"));
     actions.MoveToElement(source).Perform(); // this is where i get the exception for second element.
                       
                        actions.DoubleClick(source).Perform(); // this is where i get the exception if i comment above line
        }
    catch (StaleElementReferenceException stale)
                    {IWebElement source = driver.FindElement(By.XPath("//div[contains(text(),'" + ItemName+ "')]"));
    actions.DoubleClick(source).Perform(); // this is where i get the exception 
    }

Here is the HTML: 
<div><div draggable="true"><li style="position: relative;">
<i title="OrganizationDepartment"></i>OrganizationDepartment
<a title="Drag Me" style="cursor: grab; position: absolute; right: 0px; top: 5px;">
<button type="button" class="ms-Button ms-Button--icon iconButton root-124" data-is-focusable="true" style="cursor: grab;">
<div class="ms-Button-flexContainer flexContainer-114">
<i data-icon-name="DragObject" class="ms-Button-icon icon-126" role="presentation"></i></div></button></a></li></div></div>
<div><div draggable="true"><li style="position: relative;">
<i title="Organization"></i>Organization
<a title="Drag Me" style="cursor: grab; position: absolute; right: 0px; top: 5px;">
<button type="button" class="ms-Button ms-Button--icon iconButton root-124" data-is-focusable="true" style="cursor: grab;">
<div class="ms-Button-flexContainer flexContainer-114">
<i data-icon-name="DragObject" class="ms-Button-icon icon-126" role="presentation"></i></div></button></a></li></div></div>
1

There are 1 answers

0
Arsalan Khan On

I found the solution. It is not the element but the actions object which was getting staled on second iteration of the loop. I had to re-create the Actions object again and then do this: NewactionsObject.MoveToElement(source).Perform()