【发布时间】: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