Selenium - stale element reference: element is not attached to the page

后端 未结 4 1347
刺人心
刺人心 2020-12-20 12:57

I am trying to understand why it gives an error while I am trying to display items that took from websites. I am also using google chrome for a browser.

chro         


        
相关标签:
4条回答
  • 2020-12-20 13:01

    This type of error is usually caused by an updated DOM. I would suggest that you on all pages that contain javascript/ajax calls actually wait for those calls to finish before interacting with the site. I generally perform this wait just after the page has loaded, and when performing some kind of action that triggers javascript/ajax calls and/or updates to the DOM. In practice, this means running the below function after the page has loaded (or has been interacted with), and after that find the element you want to interact with.

    public void WaitForJqueryAjax() {
            int delay = MaxdelaySeconds;
            while(delay > 0) {
                Thread.Sleep(1000);
                var jquery = (bool)(this.driver as IJavaScriptExecutor)
                    .ExecuteScript("return window.jQuery == undefined");
                if(jquery) {
                    break;
                }
                var ajaxIsComplete = (bool)(this.driver as IJavaScriptExecutor)
                    .ExecuteScript("return window.jQuery.active == 0");
                if(ajaxIsComplete) {
                    break;
                }
                delay--;
            }
        }
    
    0 讨论(0)
  • 2020-12-20 13:16

    I had initially this problem but I got it resolved by few tricks

    • Wait for some parent element to be visible in the page (may be a parent container div)
    • Wait of any ajax (xhr) request to complete around the element you are trying to use
    • (this is the trick) do not store any element as instance property value, always fetch on request (this will help you from staleelement). Try expression in c# 6, example private IWebElement _parentContainer => _driver.FindElement(...)

    Also, you can create abstract basecontrol and some extension methods to for IWebElement and do some basic check operations while finding elements

    0 讨论(0)
  • 2020-12-20 13:22

    I have been to a similar situation.Here the main problem is page needs time so I added time.sleep(sec) and it worked fine.

    0 讨论(0)
  • 2020-12-20 13:24

    I haven't worked in c# but have worked on java/selenium. But,I can give you the idea to overcome staleness.

    Generally we will be getting the Stale Exception if the element attributes or something is changed after initiating the webelement. For example, in some cases if user tries to click on the same element on the same page but after page refresh, gets staleelement exception.

    To overcome this, we can create the fresh webelement in case if the page is changed or refreshed. Below code can give you some idea.(It's in java but the concept will be same)

    Example:

     webElement element = driver.findElement(by.xpath("//*[@id='StackOverflow']"));
     element.click();
     //page is refreshed
     element.click();//This will obviously throw stale exception
    

    To overcome this, we can store the xpath in some string and use it create a fresh webelement as we go.

    String xpath = "//*[@id='StackOverflow']";
    driver.findElement(by.xpath(xpath)).click();
    //page has been refreshed. Now create a new element and work on it
    driver.fineElement(by.xpath(xpath)).click();   //This works
    

    In this case, we are collecting a group of webelements and iterating to get the text. But it seems there is some changes in the webelement after collecting the webelements and gettext throws staleness. We can use a loop and create the element on the go and get text.

    for(int i = 0; i<5; i++)
    {
       String value = driver.findElement(by.xpath("//.....["+i+"]")).getText);
       System.out.println(value);
    }
    

    Hope this helps you. Thanks.

    0 讨论(0)
提交回复
热议问题