javascript 如何处理量角器中未找到元素的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22550329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle element not found exception in Protractor
提问by mohit
Just like Selenium webdriver provides various Exception handling for Java, is there any way we can achieve same using Protractor.
就像 Selenium webdriver 为 Java 提供各种异常处理一样,我们有什么方法可以使用 Protractor 实现相同的目标。
If we want to handle element not found exception, then what is the best way to do it using Protractor?
如果我们想处理 element not found 异常,那么使用 Protractor 的最佳方法是什么?
回答by Leo Gallucci
Answer to this question is now in Protractor's FAQ
这个问题的答案现在在量角器的常见问题解答中
How can I catch errors such as ElementNotFound?
如何捕获诸如 ElementNotFound 之类的错误?
WebDriver throws errors when commands cannot be completed - e.g. not being able to click on an element which is obscured by another element. If you need to retry these actions, try using webdriverjs-retry. If you would just like to catch the error, do so like this
当命令无法完成时,WebDriver 会抛出错误 - 例如,无法单击被另一个元素遮挡的元素。如果您需要重试这些操作,请尝试使用webdriverjs-retry。如果您只想捕获错误,请这样做
Adapted to your question:
适应你的问题:
elm.isPresent().then(function(present) {
/* no webdriver js errors here */}
if (present) {
/* element exists */
} else {
/* element doesn't exist */
}
, function(err) {
/* error handling here, i.e. element doesn't if got ElementNotFound
but, eventually and less likely, other issues will fall in here too like
NoSuchWindowsError or ElementStaleError etc...
*/
});
回答by bob.mazzo
Kudos to @Leo Gallucci for his adaptation to the OP's question:
感谢@Leo Gallucci 对 OP 问题的适应:
I faced this issue today and was hoping to go for a clean-looking solution like this :
我今天遇到了这个问题,并希望寻求一个看起来很干净的解决方案:
/* Function to check for three possible DOM elements; return the element which exists, and get the text contents. */
this.getMySelector = function(){
if (element(by.css('.mySelector')).isPresent()){
return element(by.css('.mySelector'));
}
else if (element(by.css('.mySelector2')).isPresent()){
return element(by.css('.mySelector2'));
}
else{
return element(by.css('.mySelector3'));
}
}
however, it was always hitting the first if()
and never checking other conditions. Turns out I needed to chain the promises for my scenario:
然而,它总是命中第一个if()
并且从不检查其他条件。结果我需要为我的场景链接承诺:
this.getMySelector = function(){
element(by.css('.mySelector')).isPresent().then(function (pres) {
if (pres){
defer.fulfill( by.css('.mySelector')).getText() );
}
else{
element(by.css('.mySelector2')).isPresent().then(function (pres) {
if (pres){
defer.fulfill(..);
}
}
}
}
}
// From calling test-spec.js file
getMySelector.then(function(text)){
console.log('Now I got the text ==> ' + text);
}
回答by Sakshi Singla
Try, Catch has the following syntax in Protractor. The below code will first find an element by Id 'IdTextBoxCode'. Then the code to enter code 'codeTextBox.sendKeys(code);' is in TRY block. If the code throws exception(in this case, if the element with Id 'IdTextBoxCode' is not found), then it will go to the catch block and the error handling function.
试试看,Catch 在量角器中有以下语法。下面的代码将首先通过 Id 'IdTextBoxCode' 找到一个元素。然后输入code 'codeTextBox.sendKeys(code);' 在 TRY 块中。如果代码抛出异常(在这种情况下,如果没有找到带有 Id 'IdTextBoxCode' 的元素),那么它将转到 catch 块和错误处理函数。
browser.driver.findElement(by.id(browser.params.loginPage.IdTextBoxCode)).then(function(codeTextBox)
{
try
{
console.log("Entering Code: "+code);
codeTextBox.sendKeys(code);
}
catch(err) {
console.log('In catch block');
}
}, function(err) {
console.info('Code Text Box not displayed on page. Proceeding with default Code');
});