Nightwatch: Error while running .locateMultipleElementsByElementId() protocol action: stale element reference: element is not attached to the page document

Created on 2 Apr 2019  路  11Comments  路  Source: nightwatchjs/nightwatch

version : nightwatch-1.0.18,
code :

let apiMallPageObject = browser.page.cmv.apiMallPageObject();
let homePage = apiMallPageObject.section.homePage;
let interfaceDetails = homePage.section.interfaceDetails;
homePage.clickElement('@apiAdd');
browser.pause(10000)
interfaceDetails.waitForElementVisible('@breadcrumbNavigation')

result :
Error while running .locateMultipleElementsByElementId() protocol action: stale element reference: element is not attached to the page document

This problem arises when I use page objects, which are double sections. What should I do?

stale

Most helpful comment

I'm facing the same issue on 1.3.4,
after digging up and investigating this issue, I found that nested elements can throw these errors if one of the parent sections return multiple results.

Nightwatch uses a Recursive strategy, and when the result status is OK but returns with empty results (value = []), it's calling the protocol function with null value. causing the request to look like this:

/wd/hub/session/abcd...../element/null/elements

and throw the stale object error.

Logs:

  Response 200 POST http://qaselgr01:4444/wd/hub/session/febaf4cbed3445fb27cc6636c0d318704c461273f42e46473359fa28da755119/element/0.964003393890752-16/elements (63ms) 
{ 
     sessionId: ['4c461273f42e46473359fa28da755119', 
     status: 0, 
     value: [ 
       { ELEMENT: ['0.964003393890752-21' }, 
       { ELEMENT: ['0.964003393890752-22' }, 
       { ELEMENT: ['0.964003393890752-23' } 
     ] 
} 
Warning: More than one element (3) found for element <Section [name=AgentActionsWidgetContainer]> with selector: "div.AgentActionsWidgetContainer". Only the first one will be used. 
PARSED VALUE true Section [name=AgentActionsWidgetContainer] 0.964003393890752-21 { status: 0, value: '0.964003393890752-21' } 
#####===>>> locateMultipleElementsByElementId 0.964003393890752-21 css selector div.right_pane_widget_body 
Request POST http://qaselgr01:4444 /wd/hub/session/febaf4cbed3445fb27cc6636c0d318704c461273f42e46473359fa28da755119/element/0.964003393890752-21/elements   
{ using: 'css selector', value: 'div.right_pane_widget_body' } 
  Response 200 POST http://qaselgr01:4444/wd/hub/session/febaf4cbed3445fb27cc6636c0d318704c461273f42e46473359fa28da755119/element/0.964003393890752-21/elements (62ms) 
{ sessionId: '4c461273f42e46473359fa28da755119', status: 0, value: [] } 
#####===>>> true Section [name=agentActionsWidgetContainer] null { status: 0, value: null } 
#####===>>> locateMultipleElementsByElementId null css selector div.right_pane_widget_content 
  Request POST http://qaselgr01:4444 /wd/hub/session/febaf4cbed3445fb27cc6636c0d318704c461273f42e46473359fa28da755119/element/null/elements   
{ using: 'css selector', value: 'div.right_pane_widget_content' } 
  Response 200 POST http://qaselgr01:4444/wd/hub/session/febaf4cbed3445fb27cc6636c0d318704c461273f42e46473359fa28da755119/element/null/elements (57ms) 
{ 
     sessionId: '4c461273f42e46473359fa28da755119', 
     status: m10, 
     value: { 
       message: 'stale element reference: element is not attached to the page document', 
       error: [ 
         '  (Session info: chrome=78.0.3904.70)', 
         '  (Driver info: chromedriver=78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}),platform=Linux 4.15.0-60-generic x86_64)' 
       ] 
     } 
} 
Error while running .locateMultipleElementsByElementId() protocol action: stale element reference: element is not attached to the page document 

The warning explains that the first element will be chosen, and on the page, the first index is an empty element, but the visible elements are on another index.

we are after migration from 0.9.x, and it's used to work before.
hope this info will bring some light on this issue.

All 11 comments

Was also seeing this in 1.0.19 and still see this in 1.1.0 when clicking certain input elements on a separate popup page. It isn't actually blocking the tests succeeding, but would be nice to have a solution.

How does the page object look like? Do you have verbose logs?

@beatfactor
have no detailed logs, and the result is all log,
this is page object:

module.exports = {
sections: {
homePage: {
selector: '.tarsier-breadcrumb-label',
elements: {
breadcrumbNavigation: {
selector: '//li/span[text()="API Store"]',
locateStrategy: 'xpath'
},
apiAdd: {
selector: '//div[text()="' + global.uin_geti18nByCode('CMV_NEW_INTERFACE') + '"]',
locateStrategy: 'xpath'
},
breadcrumbNavigation: {
selector: '//li/span[text()="' + global.uin_geti18nByCode('COMMON_INTERFACE_DETAILS') + '"]',
locateStrategy: 'xpath'
},
name: {
selector: '//div/input[@Placeholder="' + global.uin_geti18nByCode('CMV_CLICK_HERE_TO_EDIT_THE_API_NAME') + '"]',
locateStrategy: 'xpath',
},
des: {
selector: '//div/input[@Placeholder="' + global.uin_geti18nByCode('CMV_CLICK_HERE_TO_EDIT_API_DES') + '"]',
locateStrategy: 'xpath'
},
save: {
selector: '//div/button[text()="' + global.uin_geti18nByCode('COMMON_SAVE') + '"]',
locateStrategy: 'xpath'
},
userName_front: {
selector: '//div/span/i[text()="' + global.uin_geti18nByCode('COMMON_USERNAME') + '"]',
locateStrategy: 'xpath'
},
passWord_front: {
selector: '//div/span/i[text()="' + global.uin_geti18nByCode('COMMON_PASSWORD') + '"]',
locateStrategy: 'xpath'
},
pageNumber_front: {
selector: '//div/span/i[text()="' + global.uin_geti18nByCode('CMMON_PAGE_NUMBER') + '"]',
locateStrategy: 'xpath'
},
pageSize_front: {
selector: '//div/span/i[text()="' + global.uin_geti18nByCode('COMMON_PAGE_SIZE') + '"]',
locateStrategy: 'xpath'
},
ciCode: {
selector: '//div/span/i[text()="CI Code"]',
locateStrategy: 'xpath'
},
categoryName_front: {
selector: '//div/span/i[text()="' + global.uin_geti18nByCode('COMMON_CATEGORY_NAME') + '"]',
locateStrategy: 'xpath'
},
userName_input: {
selector: '.userName'
},
passWord_input: {
selector: '.passWord'
},
ciCode_input: {
selector: '.CICode'
},
pageNumber_input: {
selector: '//div/span/input[contains(@Class,"pageNumber")]',
locateStrategy: 'xpath'
},
pageSize_input: {
selector: '//div/span/input[contains(@Class,"pageSize")]',
locateStrategy: 'xpath'
},
categoryName_input: {
selector: '.categoryName'
},
test: {
selector: '//button[text()="' + global.uin_geti18nByCode('COMMON_TEST') + '"]',
locateStrategy: 'xpath'
},
code: {
selector: '//button[text()="code"]',
locateStrategy: 'xpath'
},
downLoadJson: {
selector: '//button[text()="' + global.uin_geti18nByCode('COMMON_DOWNLOAD') + '"]',
locateStrategy: 'xpath'
},
apiTest: {
selector: '//header/span[text()="' + global.uin_geti18nByCode('COMMON_INTERFACE_TEST') + '"]',
locateStrategy: 'xpath'
},
callStatistics: {
selector: '//header/span[text()="' + global.uin_geti18nByCode('COMMON_CALL_STATS') + '"]',
locateStrategy: 'xpath'
},
relationshipTraversal: {
selector: '//ul/li[text()="' + global.uin_geti18nByCode('CMV_RELATIONSHIP_TRAVERSAL') + '"]',
locateStrategy: 'xpath'
},
impactAnalysis: {
selector: '//ul/li[text()="' + global.uin_geti18nByCode('COMMON_IMPACT_ANALYSIS') + '"]',
locateStrategy: 'xpath'
},
rootCauseAnalysis: {
selector: '//ul/li[text()="' + global.uin_geti18nByCode('CMV_ROOT_CAUSE_ANALYSIS') + '"]',
locateStrategy: 'xpath'
},
relationshipTraversal_url: {
selector: function () {
let launch_url = global.uin_launch_url;
return '//div/p[text()="' + launch_url.substr(0, launch_url.lastIndexOf(":")) + ':1526/cmdb/appMall/friend/' + '"]'
}(),
locateStrategy: 'xpath'
},
impactAnalysis_url: {
selector: function () {
let launch_url = global.uin_launch_url;
return '//div/p[text()="' + launch_url.substr(0, launch_url.lastIndexOf(":")) + ':1526/cmdb/appMall/impact/' + '"]';
}(),
locateStrategy: 'xpath'
},
rootCauseAnalysis_url: {
selector: function () {
let launch_url = global.uin_launch_url;
return '//div/p[text()="' + launch_url.substr(0, launch_url.lastIndexOf(":")) + ':1526/cmdb/appMall/cause/' + '"]';
}(),
locateStrategy: 'xpath'
},
requestTargetInput: {
selector: '//div/input',
locateStrategy: 'xpath',
index: 3
},
relevance: {
selector: '//div/p[text()="' + global.uin_geti18nByCode('COMMON_RELEVANCE') + '"]',
locateStrategy: 'xpath'
},
requestTarget: {
selector: '//div/p[text()="' + global.uin_geti18nByCode('COMMON_REQUEST_TARGET') + '"]',
locateStrategy: 'xpath'
},
requestType: {
selector: '//div/p[text()="' + global.uin_geti18nByCode('COMMON_REQUEST_TYPE') + '"]',
locateStrategy: 'xpath'
},
POST: {
selector: '//div/p[text()="POST"]',
locateStrategy: 'xpath'
},
requestURL: {
selector: '//div/p[text()="' + global.uin_geti18nByCode('COMMON_REQUEST_URL') + '"]',
locateStrategy: 'xpath'
},
pleaseChoose: {
selector: '//div/span[text()="' + global.uin_geti18nByCode('COMMON_PLEASE_CHOOSE') + '"]',
locateStrategy: 'xpath'
},
pleaseEnterCustomURL: {
selector: '//p/input[@Placeholder="' + global.uin_geti18nByCode('CMV_PLEASE_ENTER_CUSTOM_URL') + '"]',
locateStrategy: 'xpath'
},
copyURL: {
selector: '//div/button[text()="' + global.uin_geti18nByCode('COMMON_COPY_URL') + '"]',
locateStrategy: 'xpath'
},
noData: {
selector: '//div/p[text()="' + global.uin_geti18nByCode('COMMON_NO_DATA') + '"]',
locateStrategy: 'xpath'
},
relevance_pleaseChoose: {
selector: '//div/div/span[text()="' + global.uin_geti18nByCode('COMMON_PLEASE_CHOOSE') + '"]',
locateStrategy: 'xpath'
},
pleaseEnter1: {
selector: '//span/input[@Placeholder="' + global.uin_geti18nByCode('COMMON_PLEASE_ENTER') + '"]',
locateStrategy: 'xpath',
index: 0
},
pleaseEnter2: {
selector: '//span/input[@Placeholder="' + global.uin_geti18nByCode('COMMON_PLEASE_ENTER') + '"]',
locateStrategy: 'xpath',
index: 1
},
pleaseEnter3: {
selector: '//span/input[@Placeholder="' + global.uin_geti18nByCode('COMMON_PLEASE_ENTER') + '"]',
locateStrategy: 'xpath',
index: 2
},
pleaseEnter4: {
selector: '//span/input[@Placeholder="' + global.uin_geti18nByCode('COMMON_PLEASE_ENTER') + '"]',
locateStrategy: 'xpath',
index: 3
},
},
}
}
}

and more than 1 level sections has some problem, so i have to use 1 level sections

Any updates on this issue?
I encountered the same error when using:

this.expect.element("@status").text.to.contain("Running").before(100000);

version : nightwatch-1.0.19 / 1.1.8,

This issue has been automatically marked as stale because it has not had any recent activity.
If possible, please retry using the latest Nightwatch version and update the issue with any relevant details. If no further activity occurs, it will be closed. Thank you for your contribution.

I see this is closed for being stale, but I'm still seeing this occasionally (but inconsistently). I'll work on getting a tight repro and verbose logs.

@MicahLC do you have find from your end?

Hi we moved from version 0.93 to 1.24, 1.31 we get this error hundred of times per test and its very annoying, any idea on how to silence it?

@noam2112 you can set disable_error_log to true although that won't show any errors at all during the test, but the errors are shown at the end in any case. But do the test pass afterwards? Why do you think the error is occurring?

not sure if related but can also see this when using .moveTo() and .clickElement

Error while running .moveTo() protocol action: stale element reference: element is not attached to the page document

Error while running .clickElement() protocol action: stale element reference: element is not attached to the page document

I'm facing the same issue on 1.3.4,
after digging up and investigating this issue, I found that nested elements can throw these errors if one of the parent sections return multiple results.

Nightwatch uses a Recursive strategy, and when the result status is OK but returns with empty results (value = []), it's calling the protocol function with null value. causing the request to look like this:

/wd/hub/session/abcd...../element/null/elements

and throw the stale object error.

Logs:

  Response 200 POST http://qaselgr01:4444/wd/hub/session/febaf4cbed3445fb27cc6636c0d318704c461273f42e46473359fa28da755119/element/0.964003393890752-16/elements (63ms) 
{ 
     sessionId: ['4c461273f42e46473359fa28da755119', 
     status: 0, 
     value: [ 
       { ELEMENT: ['0.964003393890752-21' }, 
       { ELEMENT: ['0.964003393890752-22' }, 
       { ELEMENT: ['0.964003393890752-23' } 
     ] 
} 
Warning: More than one element (3) found for element <Section [name=AgentActionsWidgetContainer]> with selector: "div.AgentActionsWidgetContainer". Only the first one will be used. 
PARSED VALUE true Section [name=AgentActionsWidgetContainer] 0.964003393890752-21 { status: 0, value: '0.964003393890752-21' } 
#####===>>> locateMultipleElementsByElementId 0.964003393890752-21 css selector div.right_pane_widget_body 
Request POST http://qaselgr01:4444 /wd/hub/session/febaf4cbed3445fb27cc6636c0d318704c461273f42e46473359fa28da755119/element/0.964003393890752-21/elements   
{ using: 'css selector', value: 'div.right_pane_widget_body' } 
  Response 200 POST http://qaselgr01:4444/wd/hub/session/febaf4cbed3445fb27cc6636c0d318704c461273f42e46473359fa28da755119/element/0.964003393890752-21/elements (62ms) 
{ sessionId: '4c461273f42e46473359fa28da755119', status: 0, value: [] } 
#####===>>> true Section [name=agentActionsWidgetContainer] null { status: 0, value: null } 
#####===>>> locateMultipleElementsByElementId null css selector div.right_pane_widget_content 
  Request POST http://qaselgr01:4444 /wd/hub/session/febaf4cbed3445fb27cc6636c0d318704c461273f42e46473359fa28da755119/element/null/elements   
{ using: 'css selector', value: 'div.right_pane_widget_content' } 
  Response 200 POST http://qaselgr01:4444/wd/hub/session/febaf4cbed3445fb27cc6636c0d318704c461273f42e46473359fa28da755119/element/null/elements (57ms) 
{ 
     sessionId: '4c461273f42e46473359fa28da755119', 
     status: m10, 
     value: { 
       message: 'stale element reference: element is not attached to the page document', 
       error: [ 
         '  (Session info: chrome=78.0.3904.70)', 
         '  (Driver info: chromedriver=78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}),platform=Linux 4.15.0-60-generic x86_64)' 
       ] 
     } 
} 
Error while running .locateMultipleElementsByElementId() protocol action: stale element reference: element is not attached to the page document 

The warning explains that the first element will be chosen, and on the page, the first index is an empty element, but the visible elements are on another index.

we are after migration from 0.9.x, and it's used to work before.
hope this info will bring some light on this issue.

Was this page helpful?
0 / 5 - 0 ratings