【问题标题】:Selenium routine works the first time, but subsequent runs failSelenium 例程第一次工作,但后续运行失败
【发布时间】:2017-05-26 19:31:46
【问题描述】:

我有这个例程,使用 selenium-webdriver 进行 NPM - require('selenium-webdriver'):

       function run(){
          driver.get(cdthost + '/#!/home');
          driver.navigate().refresh(); // same problem, with or without this line
          let acqId = acq._id;
          assert(acqId, 'acqId is not defined.');
          // on the next line we wait to ensure that the page is loaded
          driver.wait(until.elementLocated(By.id(`open-editing-acqId-${acqId}`)), 3000);
          driver.findElement(By.id(`open-editing-acqId-${acqId}`)).click();
          let file = path.resolve($root + '/csv-data/IT-DE-Jasper.csv');
          let el = driver.findElement(By.id(`import-file-acqId-${acqId}`));
          driver.wait(until.elementIsVisible(el), 2000);
          el.sendKeys(file);
          return driver.findElement(By.id(`submit-file-acqId-${acqId}`)).click().then(function () {
            return Promise.delay(2000)
          })
          .catch(function (err) {
            console.error('\n\n', ' => Suman caught promise rejection => ', err.stack || err);
            return Promise.reject(err);
          });
       }

我调用了一次,它运行良好。我再次调用它,它在页面上找不到任何元素,即使我调用 refresh()。

知道为什么第一次会起作用,但之后就不行了吗?

【问题讨论】:

  • 您是否能够确认它第二次运行时您实际上是在您期望的页面上?您是否在每次运行它时都创建一个新驱动程序以确保没有剩余的 cookie 或之前运行的某些东西可能会使页面看起来不同?这听起来不是一个有趣的问题......
  • 哈哈,是的,不好玩;我想每次都重新创建驱动程序,但这似乎有点过头了;但是现在显式调用刷新不起作用,也许我会尝试重新创建驱动程序。
  • @mrfreester 我认为它会在正确的页面上,因为我明确调用 driver.get(cdthost + '/#!/home');,而 cdthost 是一个常量。
  • 我不认为重新创建驱动程序是正确的方法,这是矫枉过正,还有其他问题
  • 我的第一步是查看screenshot on failure。为此类情况设置屏幕截图是值得的。

标签: node.js selenium selenium-webdriver


【解决方案1】:

好的,这就解决了:

 let passCount = 0;

 // ...
function run(){
 if(passCount < 1){
    driver.get(cdthost + '/#!/home');
  }
  else{
    driver.navigate().refresh();
  }

  passCount++;
 }

有点令人失望...我将向 selenium-webdriver 人员提出问题。

另一种可行的方法(未经测试)是这样的:

let promise = null;

function getHomePage(){

  if(!promise){
     promise = driver.get(cdthost + '/#!/home');
   }

  return promise;
}

function run(){
  return getHomePage().then(function(){

  });
}

但我不确定这是更理想还是更不理想。

【讨论】:

  • 有趣,这几乎就像承诺永远不会解决,因为由于某种原因它已经在页面上,所以页面最终不会再次获取......
  • 是的,这很糟糕,因为这意味着您无法轻松并行调用 run()
  • fwiw,如果你想做并行,每个线程有一个干净的驱动程序很有意义。特别是如果您在网格或类似的服务(如 saucelabs 或 browserstack)上运行这些。这也将缓解这个问题。话虽如此,我不确定我是否完全理解这种情况,所以也许这对这种情况没有意义:)
  • 是的,不是单独的线程 - 但本质上是交错执行 - 我是 selenium 的新手,但我怀疑我是否需要为每个线程提供单独的驱动程序..但也许。
  • 嘿科里,如果你有解决方案,请发布作为答案
猜你喜欢
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-09
相关资源
最近更新 更多