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

后端 未结 4 1354
刺人心
刺人心 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--;
            }
        }
    

提交回复
热议问题