我正在使用https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs作为我的node.js项目。
请查看以下代码:
driver.get(url).then(function() {
return driver.findElement(By.css('firstSelector'));
}.then(function() {
# element 1 was found, should early exit
}, function(err) {
# element 1 was not found, keep going
}).then(function() {
return driver.findElement(By.css('secondSelector'));
}.then(function() {
# element 2 was found, should early exit
}, function(err) {
# element 2 was not found, keep going
}).then(function() {
# some other big and long function
}).then(function() {
driver.quit();
# end
});
我想要做的是在找到元素#1的情况下,我想跳到最后,基本上跳过“大和长函数”但执行driver.quit()部分。
现在,我已经阅读了很多关于此的内容以及人们建议的内容如下:在您希望“跳过”发生时发出错误并在最后执行某些操作来执行您的操作想要在完成之前做,在我的情况下'driver.quit()';
这是有道理的,但由于某些原因,nodejs中的selenium不允许我这样做。具体来说,我试过这个:
driver.get(url).then(function() {
return driver.findElement(By.css('firstSelector'));
}.then(function() {
# element 1 was found, should early exit
throw new Error('abort');
return null;
}, function(err) {
# element 1 was not found, keep going
}).then(function() {
return driver.findElement(By.css('secondSelector'));
}.then(function() {
# element 2 was found, should early exit
}, function(err) {
# element 2 was not found, keep going
}).then(function() {
# some other big and long function
}).then(function() {
driver.quit();
# end
}).catch(function(err) {
driver.quit();
});
但捕获部分永远不会被执行!我也试过以下一行:
webdriver.promise.reject()
再一次,没有!
基本上我感兴趣的是 - 为了让它跳过catch()并调用那部分,我需要编写什么代码?
谢谢!