我正在寻找容器中的“更多”链接,以继续单击它,直到链接不再存在为止。当不再有“更多”的链接可用时,我正在创建一个延迟的,并返回它与履行调用发生。
.then(function (previousResults) {
var deferred = webdriver.promise.defer();
// look for the more link, keep clicking it till it's no longer available
browser.wait(function() {
// see if we have "more" to click on
browser.findElements(byMoreLinkXpath)
.then(function (moreLinks) {
if (moreLinks[0]) {
console.log('has more');
moreLinks[0].click()
.then(function() {
// check for spinner to go away
browser.wait(pageDoneLoading, configSetting.settings.testTimeoutMillis);
});
} else {
console.log('no more');
deferred.fulfill(true);
}
});
}, 5000);
return deferred.promise;
})不幸的是,承诺从来没有实现过我尝试在return deferred.promise;块中执行else,虽然它适用于reject,但仍然不适用于fulfill。
发布于 2015-06-03 01:37:18
webdriver.wait的语法
电文)
但是在您的代码中,第一个参数既不是condition,也不是promise,而是function,所以我会将其更改为:
另外,我认为您在这里所做的是承诺反模式(而且我没有看到再次检查更多链接的循环,对不起,我认为您还没有完全理解driver.wait ),我只想将上面的功能简化为:
function exhaustMoreLinks(){
return driver.wait( until.elementLocated(byMoreLinkXpath), 5000)
.then(function(){
return driver.findElement(byMoreLinkXpath);
}).then(function(moreLink){
console.log('more links');
return moreLink.click();
}).then(function(){
return browser.wait(pageDoneLoading(), configSetting.settings.testTimeoutMillis).then(exhaustMoreLinks);
}, function(err){
if(err.name === 'NoSuchElementError' || (err.message.search(/timed out/i)> -1 && err.message.search(/waiting element/i) > -1) ){ // checking if error because time-out or element not found, if true, redirect to fulfil
console.log('no more links');
return;
}else{
throw err;
}
});
}使用方式类似于:
...
.then(exhaustMoreLinks)
...https://stackoverflow.com/questions/30605820
复制相似问题