【问题标题】:StaleElementReferenceError: stale element reference: element is not attached to the page documentStaleElementReferenceError:过时的元素引用:元素未附加到页面文档
【发布时间】:2019-03-25 14:28:09
【问题描述】:

我在其中一个量角器页面中创建了一个函数。单击该功能后,我将转到另一个我使用了 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

【问题讨论】:

  • obj[i].click(); in for 循环导致当前页面更改,下一个obj.click() 在找到它的页面上不起作用。所以举报StaleElementReferenceError
  • 有什么方法可以突破并返回下一页..我在循环中使用了 break..return 但没有运气
  • 是只点击第一个匹配的&lt;a&gt;还是一个一个点击所有匹配的&lt;a&gt;

标签: javascript protractor


【解决方案1】:
  1. 通过element.all().getText()阅读所有找到的链接的文本
  2. 通过比较链接的文本和参数找出匹配链接的索引:str
  3. 再次搜索所有链接并通过element.all().get(index)点击匹配的链接
  4. 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);
  });
});

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    相关资源
    最近更新 更多