Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle element not found exception in Protractor

Just like Selenium webdriver provides various Exception handling for Java, is there any way we can achieve same using Protractor.

If we want to handle element not found exception, then what is the best way to do it using Protractor?

like image 743
mohit Avatar asked Mar 21 '14 04:03

mohit


1 Answers

Answer to this question is now in Protractor's FAQ

How can I catch errors such as 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

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...
  */
});
like image 185
Leo Gallucci Avatar answered Sep 17 '22 20:09

Leo Gallucci