![](/img/trans.png)
[英]Protractor - Failed: stale element reference: element is not attached to the page document
[英]StaleElementReferenceError: stale element reference: element is not attached to the page document
我已經在量角器頁面之一中創建了一個函數。 在單擊功能時,將帶我到我使用了require關鍵字的另一頁。
this.naviagteToSwaggerOrReadme = function(str) {
this.endpointDropdown.click();
browser.sleep(2000);
//element(by.partialLinkText('swagger/index.html')).click();
element(
by.className('popover ng-scope ng-isolate-scope bottom fade in')
)
.all(by.tagName('a')).then(function(obj) {
console.log('Number of elements with the a tag in the parent element is ' + obj.length);
for (let i = 0; i < obj.length; i++) {
obj[i].getText().then(function(text) {
console.log('The text on the link is ' + text);
var linkText = text;
if (linkText.toLowerCase().indexOf(str) !== -1) {
obj[i].click();
} else {
obj[i + 1].click();
}
})
}
});
browser.sleep(5000);
return require('./Swagger.js');
};
我正在調用相同的功能。
swagger = appsPageOne.naviagteToSwaggerOrReadme('swagger');
swagger.getSwaggerTitle().then(function(title){
console.log('This is the title '+title);
})
但是我得到的錯誤是:
Message:
Failed: stale element reference: element is not attached to the page document
(Session info: chrome=74.0.3729.6)
(Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.15063 x86_64)
Stack:
StaleElementReferenceError: stale element reference: element is not attached to the page document
element.all().getText()
讀取所有找到的鏈接的文本 str
進行比較,找出匹配的鏈接的索引 element.all().get(index)
單擊匹配的鏈接 return require('./Swagger.js');
在then()
內部,否則return
在單擊匹配的鏈接之前執行的語句。 更改代碼如下:
this.naviagteToSwaggerOrReadme = function(str) {
this.endpointDropdown.click();
browser.sleep(2000);
let links = element.all(
by.css('.popover.ng-scope.ng-isolate-scope.bottom.fade.in a')
)
return links.getText().then(function(txts) {
let size = txts.length;
console.log('Number of elements with the a tag in the parent element is ' + size);
let index = txts.findIndex(function(txt){
return txt.toLowerCase().includes(str);
});
if(index === -1) {
index = size - 1;
}
links.get(index).click();
browser.sleep(5000);
return require('./Swagger.js');
});
};
naviagteToSwaggerOrReadme()
返回一個promise,因此您需要在then()中消耗返回的swagger
appsPageOne.naviagteToSwaggerOrReadme('swagger').then(function(swagger){
swagger.getSwaggerTitle().then(function(title){
console.log('This is the title '+title);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.