Selenium例程第一次工作,但后续运行失败

时间:2017-05-26 19:31:46

标签: node.js selenium selenium-webdriver

我有这个例程,使用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()。

任何想法为什么这会在第一次起作用,但不是在那之后呢?

1 个答案:

答案 0 :(得分:0)

好吧,这解决了它:

// Callback to run when data is ready
function reqListener() {
  // Parse the JSON text to an object so we can get just one property
  var data = JSON.parse(this.responseText);
  // Append that value to the DOM.
  document.querySelector("#demo").innerHTML = data.body;
}

// Create a new ajax requst
var oReq = new XMLHttpRequest();
// Fire callback on load event
oReq.addEventListener("load", reqListener);
// Create the connection to our API
oReq.open("GET", "https://jsonplaceholder.typicode.com/posts/1");
// Fire the request
oReq.send();

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

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

 let passCount = 0;

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

  passCount++;
 }

但我不确定这是否更理想,或更不理想。