量角器:失败:陈旧元素引用:单击后没有将元素附加到页面文档()

问题描述 投票:0回答:1

当我试图点击与给定文本匹配的下拉元素时,我面临上述问题。下拉列表的所有选项都与包含文本的span的图像一样。我尝试了这里给出的东西,但没有成功。 Protractor: Failed: stale element reference: element is not attached to the page document。标签的参考如下图所示。 The tags used are <ul><li>.

我的代码看起来像这样

element.all(by.css('ul[class='ui-dropdown-items ui']>li')).each(function(element) {
  element.getText().then(function (text) {
     if (text.includes("Bag")){
       element.click();
     }
  });
});

虽然点击动作由上面的语句执行,但仍然会抛出错误。此外,当我尝试点击任何索引作为硬编码时,它没有错误。 element.all()。get(4),其中4是元素的索引。我的申请是有角度的5.有人可以帮我解决这个问题!这阻碍了我的项目。

protractor each
1个回答
2
投票

element.all().each()对每个元素执行迭代,即使你添加if条件只点击一个元素中的一个元素,但每次迭代中的getText()仍然被执行。

单击匹配的元素后,页面将重定向或刷新。然后在“新”页面上的下一个元素上调用getText(),这就是报告stale reference exception的原因

您需要过滤匹配的元素,然后单击它。

// approach 1
element
.all(by.css('ul[class='ui-dropdown-items ui']>li'))
.filter(function(ele) {
  return ele.getText().then(function (text) {
     return text.includes("Bag");
  });
})
.then(function(eles){
    if (eles && eles.length > 0) {
        eles[0].click()
    } 
})

// approach 2
let options = element.all(by.css('ul[class='ui-dropdown-items ui']>li'))

options.getText(function(txts) {
  return txts.findIndex(function(txt){
    return txt.includes('Bag');
  })
})
.then(function(index){
    return index !== -1 && options.get(index).click();
})
© www.soinside.com 2019 - 2024. All rights reserved.