0
votes
   element.all(by.css('#options-ul li')).each(function (elem, index) {
                return elem.evaluate('option').then(function (value) {
                    if (correctAns.indexOf(value.Id)!==-1) {
                        elem.click();
                        browser.sleep(5000);
                       quizPageElements.clickNextQn();
                        // browser.sleep(15000);
                        browser.waitForAngular();
                    }
                });

            });

Getting error like

Failed: stale element reference: element is not attached to the page document
  (Session info: chrome=60.0.3112.113)
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.3.9600 x86_64)
StaleElementReferenceError: stale element reference: element is not attached to the page document
  (Session info: chrome=60.0.3112.113)
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.3.9600 x86_64)
    at WebDriverError (C:\Users\Mvision\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\error.js:27:5)
    at StaleElementReferenceError (C:\Users\Mvision\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\error.js:227:5)
    at Object.checkLegacyResponse (C:\Users\Mvision\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\error.js:505:15)
    at parseHttpResponse (C:\Users\Mvision\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\http.js:509:13)
    at doSend.then.response (C:\Users\Mvision\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\http.js:440:13)
    at process._tickCallback (internal/process/next_tick.js:109:7)

Tried getting each element inside <ul> tag and get id of the scope variable using evaluate function in protractor and checking the condition and then clicking the option element and then clicking next button.Sometimes getting this error in evaluate function.

1

1 Answers

0
votes

Since couldn't avoid stale error, I just tried catching error in evaluate function and proceed to click element if an error happens.Like,

element.all(by.css('#options-ul li')).each(function (elem, index) {
                return elem.evaluate('option').then(function (value) {
                    if (correctAns.indexOf(value.Id)!==-1) {
                        elem.click();
                        browser.sleep(5000);
                       quizPageElements.clickNextQn();
                        // browser.sleep(15000);
                        browser.waitForAngular();
                    }
                },function(err){
             //do operation to click a button
             });
         });